Hi Guys,
i am trying to setup email notification. My script goes through our filers collects errors, warrning etc and sends the email.
the script and email notification is working fine, the short version of script is listed below:
*************
#$err = Get-Content \\filer\c$\etc\messages | Select-String error | Write-Host -ForegroundColor red
#$warn = Get-Content \\filer\c$\etc\messages | Select-String warning | Write-Host -ForegroundColor yellow
#$suc = Get-Content \\filer\c$\etc\messages | Select-String success | Write-Host -ForegroundColor green
$body = @()
$body +=Get-Content \\filer\c$\etc\messages | Select-String error
$body +=Get-Content \\filer\c$\etc\messages | Select-String warning
$body +=Get-Content \\filer\c$\etc\messages | Select-String success
$body = $body | out-string
write-host $body
$email = @{
From = "filer@comp.org"
To = "me@comp.org"
Subject = "Messages from filer"
SMTPServer = "smtp.comp.org"
Body = $body
}
send-mailmessage @email
*******
now if i try to use same script and color code the output red for error, yellow for warning its running fine from command line but it errors out when i try to send the email:
This is the script:
#$err = Get-Content \\filer\c$\etc\messages | Select-String error | Write-Host -ForegroundColor red
#$warn = Get-Content \\filer\c$\etc\messages | Select-String warning | Write-Host -ForegroundColor yellow
#$suc = Get-Content \\filer\c$\etc\messages | Select-String success | Write-Host -ForegroundColor green
$body = @()
$body +=Get-Content \\filer\c$\etc\messages | Select-String error | Write-Host -ForegroundColor red
$body +=Get-Content \\filer\c$\etc\messages | Select-String warning | Write-Host -ForegroundColor yellow
$body +=Get-Content \\filer\c$\etc\messages | Select-String success | Write-Host -ForegroundColor green
$body = $body | out-string
write-host $body
$email = @{
From = "filer@comp.org"
To = "me@comp.org"
Subject = "Messages from filer"
SMTPServer = "smtp.comp.org"
Body = $body
}
send-mailmessage @email
and error is :
nd-MailMessage : Cannot validate argument on parameter 'Body'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At line:20 char:17
+ send-mailmessage <<<< @email
+ CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
not sure how my argument gets "null or empty" when i add the color formating.
hopefully somebody can help
thx a lot