If / Else problems

I am a problem with an if/else part of my script. The script is designed to send an email after comparing two text files of users to determine if any changes have been made and report back those changes.

The script was working fine and showing the correct output but I can’t get it to email me. During testing I have tried this

 $body -eq $null 

The above returns true. The below returns False. $body -ne $null

So therefore as I understand it my if statement should activate and then should my else statement should not. However it seems to be trying to run my else statement afterwards and returns an error on the parameter ‘Body’. Saying it is null or empty please provide an argument that is not null or empty.

The problem here is I know it is null so I don’t want it to try and execute at this time.

 $body = $compare | select @{l='User';e={$_.InputObject}}, @{l='Added/Removed';e={$_.SideIndicator}} | Format-Table | Out-String if ($body -eq $null) { Send-MailMessage -to "xxx@xxx.com" -from "yyy@yyy.com" -subject "changes" -Body "There have been no changes in the last 24 hours" -SmtpServer "abc" } else ($body -ne $null) { Send-MailMessage -to "xxx@xxx.com" -from "yyy@yyy.com" -subject "zzz" -Body "$body" -SmtpServer "abc" } 

submitted by /u/joe297
[link] [comments]

Leave a Reply