$EmailFrom = "xxxxxxx@hotmail.com" $EmailTo = "xxxxxxx@hotmail.com" $Subject = "Checking on Rename Shop Daily Backup on WDMyCloud" # This section checks to see if task scheduler returned a value of 0 # If the value = 0 continue otherwise send email and exit $filter = @{ Logname = 'microsoft-windows-taskscheduler/operational' ID = 201 Data = '\My Tasks\SC_Rename Shop Daily Backup Monthly' } $q = Get-WinEvent -MaxEvents 1 -FilterHashtable $filter | Select @{n='ResultCode';e={$_.Properties[3].Value}} $z = $q.psobject.Properties.value echo return = $z if ($z -ne 0) { $Body = " Task Scheduler has returned error = $z. Check the logs on Main" $SMTPServer = "127.0.0.1" $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xxxxxxx@hotmail.com", "yyyyyy") $SMTPClient.Send($EmailFrom, $EmailTo, $Subject,$body) exit } #======================================================= $Event = Get-WinEvent -computername main -FilterHashtable @{Logname='microsoft-windows-backup';Id=14} -MaxEvents 1 # Convert the event to XML $eventXML = [xml]$Event.ToXml() $test = $eventxml.Event.EventData.Data | Where-Object {$_.name -eq "Backupwriteendtime"} | select -expandProperty '#text' $test.Substring(23,10) $x = Get-Date -Format o $y = $x.substring(0,10) $d = Get-Date -DisplayHint Date # This part runs to pass $s to the elseif statement $t = $eventxml.Event.EventData.Data | Where-Object {$_.name -eq "Errormessage"} $r,$s = $t.'#text' -split "%%" ,2 #-------------------------------------------------------------------- # This section checks to see if Backup date is Today # If the dates don't match it sends an email then exits otherwise it continues if ($test.substring(23,10) -notmatch $y) { $Body = " The last backup date should be today ($d). Check the logs on Main" $SMTPServer = "127.0.0.1" $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xxxxxxxx@hotmail.com", "yyyyyyyyy") $SMTPClient.Send($EmailFrom, $EmailTo, $Subject,$body) exit } Exception calling "Send" with "4" argument(s): "Failure sending mail." At C:\Users\zzzz\Documents\WindowsPowerShell\Scripts\test.ps1:66 char:5 + $SMTPClient.Send($EmailFrom, $EmailTo, $Subject,$body) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SmtpException