I created a script to ping servers that runs from Task Scheduler. If a server does not ping, it is supposed to send me an email. It also sends an email once per day that it is alive.
The problem is that most of the emails get sent and arrive as expected, but occasionally an email does not get sent. There is no error generated. Based on mail flow analysis, the email never appears to reach our Office365 Exchange server.
I've read various solutions, including inserting sleep cmdlets and using a try/catch block. So far I've only found posts with issues where the Send-MailMessage fails consistently, but no posts with a problem where it fails erratically.
I'm logging the results, but as far as the try/catch is concerned, the Send-MailMessage always succeeds. My log files always show a successful send, never a failure.
My PowerShell skills are still in the very beginner stage, all of the code for decrypting userid and password have been copied from other more knowledgeable people.
Code that reads credentials, decrypts credentials, construct and send email:
(emailOperator is a hybrid Office365 user account with an E1 license)
100% of the time, if I manually run the script from PowerShell ISE, the email is sent.
100% of the time, if I trigger the job manually from Task Scheduler, the email is sent (I've done this dozens of times, not hundreds).
$EmailRecipient = '[email protected]'
$emailFrom = '[email protected]'
$ReportingHost = 'Reporting Host: ' + $env:computername
$CredentialLocation = "c:\MyServer\SendEmailUsingOffice365CredentialFolder\"
#Read the AES Key
$AES_AdminDomain_KeyFilePath = $CredentialLocation + "AESKEY.txt" <# AES Admin Domain Encryption Key #>
$AESKey = Get-Content $AES_AdminDomain_KeyFilePath
#Read and decrypt The Password using the AESKey
$ExportedPasswordFilePath = $CredentialLocation + "Password.txt" <# Password #>
$pwdTxt = Get-Content $ExportedPasswordFilePath
$securePwd = $pwdTxt | ConvertTo-SecureString -Key $AESKey
#Read and decrypt the username using the AESKey
$ExportedUserNameFilePath = $CredentialLocation + "User.txt" <# Username #>
$UserSecureString = ConvertTo-SecureString -key $AESKey -String (Get-Content $ExportedUserNameFilePath)
$Pointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($UserSecureString)
$Sender = [Runtime.InteropServices.Marshal]::PtrToStringAuto($Pointer)
#generate the credentials
$EmailCredentials = New-Object System.Management.Automation.PSCredential -ArgumentList $Sender, $securePwd
#Build the email
$EmailSubject = $env:ComputerName + " is alive and checking endpoints at " + (get-date -Format HH:mm)
$EmailBody = "- end - "
Start-Sleep -s 1
Try
{
Send-MailMessage -SmtpServer smtp.office365.com -Port 587 -UseSSL -From $EmailFrom -To $EmailRecipient -Subject $EmailSubject -Body $EmailBody -Credential $EmailCredentials
}
Catch
{
" error, I am alive email NOT sent, x" + $_.Exception.Message + "x " + $(Get-Date -format 'MM/dd/yyyy,HH:mm:ss') | Out-File -Filepath $Log_File -NoClobber -append
}
" eMail, I am alive email sent, y" + $_.Exception.Message + "y " + $(Get-Date -format 'MM/dd/yyyy,HH:mm:ss') | Out-File -Filepath $Log_File -NoClobber -append
Is there a method to send the email and have PowerShell trace its progress?
thank you