universalvef.blogg.se

Powershell monitor folder for new files and send email
Powershell monitor folder for new files and send email







powershell monitor folder for new files and send email
  1. #POWERSHELL MONITOR FOLDER FOR NEW FILES AND SEND EMAIL FULL#
  2. #POWERSHELL MONITOR FOLDER FOR NEW FILES AND SEND EMAIL PASSWORD#
powershell monitor folder for new files and send email

Out-File -FilePath "$scriptFolderPath\$scriptTitle_$currentDate.log" -Append $currentDate = Get-Date -Format "MM-dd-yyyy" $logTime = Get-Date -Format "MM-dd-yyyy HH:mm"

#POWERSHELL MONITOR FOLDER FOR NEW FILES AND SEND EMAIL FULL#

See below for an example of what our full code should now look like: $scriptFolderPath = "C:\Scripts\Monitoring" This takes whatever the errorCount variable currently contains and adds 1 to it. In the code sections that contain the error message and writeLog function for the error message, we want to add new lines to add 1 to our error count variable. Next, we will scroll down our script to where we were checking if our service and our application are running.

#POWERSHELL MONITOR FOLDER FOR NEW FILES AND SEND EMAIL PASSWORD#

$from = "EMAIL ADDRESS FOR THE ACCOUNT YOU SAVED THE PASSWORD FOR" $encrypted = Get-Content encryptedPass.txt | ConvertTo-SecureString $username = "EMAIL ACCOUNT YOU SAVED THE PASSWORD FOR" $scriptFolderPath = "C:\Scripts\Monitoring" This will help us keep track of whether or not an error is detected as well as how many errors are detected.

powershell monitor folder for new files and send email

We will also want to add an $errorCount variable and set it to 0. I will be showing the email settings I am using, but remember that yours may be different depending on which email service you are using. We can add an Email Settings list of variables to the top of our script, below our $scriptTitle and $scriptFolderPath variables. Now we can begin configuring our email settings, using the encrypted text file we created. Something like works great! Adding the Email Alert While this is not required, I highly recommend it if you have an available license/mailbox. $credential.Password | ConvertFrom-SecureString | Set-Content encryptedPass.txtĪs an extra layer of precaution, I usually create a service mailbox that is only used for sending emails in my account and will not store any confidential incoming email. We will then use the Get-Credential cmdlet to be prompted to enter the e-mail account username and password that we want to use when sending the email. Open a separate PowerShell window and navigate to where we are running the script, in this case "C:\Scripts\Monitoring". Since we will need to use a password for accessing the Microsoft 365 account, let's convert our password and store it using an encrypted string inside a text file. Storing the Email Account Passwordīefore we jump straight into the email settings, we have something important to do first. This should work with any email relay you have access to. To do this, we will be using the Send-MailMessage cmdlet and the Microsoft Office 365 service. We will fix this today by adding an email alert to our script that will email us when failures are detected. While this is good for a historical view, and potential root cause detection, it doesn't provide a way for us to be actively notified on errors. The script was left with a bit to be desired as it is only logging the data it gathers to a text file. In the first part of this series, we ran through setting up a very basic scheduled task to monitor an application process and service.









Powershell monitor folder for new files and send email