PowerShell to send email -
PowerShell to send email -
i running task scheduler execute powershell every 30 minutes.
so
$users = search-adaccount -lockedout -searchbase "ou=people,dc=example,dc=com" -searchscope subtree | select samaccountname
this returns list of users. want create emailbody users
$emailbody = foreach($user in $users) {"`r`n",$user} send-mailmessage -to $to -subject "locked accounts" -bodyashtml $emailbody -from $from -credential $cred -smtpserver $server -debug
however current code send email if no 1 locked. there anyway can check see if $users has @ to the lowest degree 1 person?
(i haven't tested $emailbody know if acceptable bodyashtml)
================
updated code
if ($users) { if ($users.count -gt 0) {#even if there users, line false. # send email here foreach($user in $users) { $message = $message + " " + $user + " locked out" + "`r`n" write-host $user } send-mailmessage -to $to -subject "locked accounts" -bodyashtml $message -from $from -credential $cred -smtpserver $server -debug } }
if ($users -ne $null) # lists can empty well, in case count fails { if ($users.count -gt 0) { # send email here } }
powershell powershell-v4.0
Comments
Post a Comment