0

Attempting to run the follow script for a reboot I get the following error,

"Send-MailMessage : Cannot validate argument on parameter 'Body'. The argument is null or empty. Supply an arg
that is not null or empty and then try the command again.
At line:8 char:30
+             Send-MailMessage @messageParameters -BodyAsHtml
+                              ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage"

Any help would be great!

Thanks Cody

Restart-Computer -ComputerName nocconverter1 -Wait -For Wmi 
$server = 'XXX'
ping -n 2 $server >$null
Function Server_Status_Check {
                if($lastexitcode -eq 0) {
            write-host "$server is ONLINE" 
        } else {
            write-host "$server is OFFLINE/UNREACHABLE"
}          
}


$messageParameters = @{                        
                Subject = "Result: Reboot report for WebPeriop  - $((Get-Date).ToShortDateString())"                        
                Body = Server_Status_Check | out-string
                From = "XXXX"                        
                To = "XXXXX"                        
                SmtpServer = "exmbx6"                        
            }                        
            Send-MailMessage @messageParameters -BodyAsHtml      

1 Answer 1

0

Write-Host writes directly to the display. It doesn't write to stdout out therefore your Server_Status_Check function outputs nothing. Change it to:

Function Server_Status_Check {
    if($lastexitcode -eq 0) {
        "$server is ONLINE" 
    } 
    else {
        "$server is OFFLINE/UNREACHABLE"
    }
}     
1
  • No problem. In general, use Write-Host very sparingly and only when you want the end user to see something that never needs to be captured to a variable or logged to a file.
    – Keith Hill
    Jul 9, 2014 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.