Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

PowerShell IF ELSE statement not working

$
0
0

I have the following script, which is to search for locked out AD accounts and it works fine, but an email also gets sent (a blank one) if there aren't any locked out accounts!

I tried putting an Else > Exit line in there but it doesn't appear to be working.

How do I resolve this?

 

Text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
Set-ExecutionPolicy 0
$a = ""
$Result=Search-ADAccount -LockedOut | ConvertTo-Html -head $a SamAccountName, LastLogonDate, LockedOut, PasswordExpired | Out-String
If ($Result)
{ Send-MailMessage -SmtpServer my.smtpserver.com -To my@email.com -From my@email.com -Subject "Locked Out Accounts" -Body $Result -BodyAsHtml
}
Else
{  
Exit
}


Viewing all articles
Browse latest Browse all 15370

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>