PowerShell Management Series (40) PowerShell query and Unlock ad account (after improvement, only send mail once)

Source: Internet
Author: User
Tags mailmessage samaccountname

-----provide ad\exchange\lync\sharepoint\crm\sc\o365 and other Microsoft product implementation and outsourcing, qq:185426445. Phone 18666943750

Requirements: according to the requirements of the group AD domain and the subordinate 3 sub-domains of all AD account statistics, query all locked accounts, unlock, and send mail every morning to point to the mailbox. (the previous 39 based on a slight adjustment, only one email can be sent)

Remove-item c:\get_locked_user\ -recurse -forceif (! ( test-path c:\get_locked_user))  {new-item c:\get_locked_user -type directory} Import-module activedirectory$yuntcloud_lockeduser=search-adaccount -lockedout -searchbase   "Dc=yuntcloud,dc=com"  -server dc01|select name,samaccountname$aaa_lockeduser= search-adaccount -lockedout -searchbase  "Dc=aaa,dc=yuntcloud,dc=com"  -server aaaDC1| select name,samaccountname$bbb_lockeduser=search-adaccount -lockedout -searchbase  "DC=BBB, Dc=yuntcloud,dc=com " -server bbbdc1|select name,samaccountname$ccc_lockeduser=search-adaccount  -LockedOut -SearchBase  "Dc=ccc,dc=yuntcloud,dc=com"  -server cccdc1|select name, samaccountname#send email$username =  "[email protected]"         #定义管理员账户名称 $Password  = ConvertTo-SecureString  "Anvf20170708"  -asplaintext –force$cred = new-object system.management.automation.pscredential ($UserName , $Password) $nFrom = "[email protected]" $nTo = "[email protected]" [email protected] () #Unlock   yuntcloud locked userif  ([String]::isnullorempty ($ ($yuntcloud _lockeduser|%{$_.name}))} { write-host  "Yuntcloud no locked user" Start-sleep 5set-content c:\get_locked_user \locked.txt -value  "Yuntcloud no locked user" #Send-mailmessage -from  $nFrom  -To  $nTo  -Subject  "Yuntcloud no locked user"  -Body  "Yuntcloud  no locked user " -Credential  $cred  -SmtpServer " mail.yuntcloud.com " - encoding  ([System.text.encoding]::utf8)}else{write-host  "Yuntcloud locked user" foreach ($i  in  $yuntcloud _lockeduser) {write-host  $i. name$lockeduser+= $i}start-sleep 5search-adaccount  -lockedout -searchbase  "Dc=yuntcloud,dc=com"  -server dc01 | unlock-adaccountwrite-host  "Yuntcloud all locked account were unlocked" $ lockeduser | Export-csv -path c:\get_locked_user\yuntcloud_lockeduser.csv -notypeinformation -encoding  utf8 add-Content C:\get_locked_user\locked.txt -Value  "yuntcloud locked  User "add-content c:\get_locked_user\locked.txt -value $ ($yuntcloud _lockeduser|%{$_.name}) # send-mailmessage -from  $nfrom  -To  $nto  -Subject  "Yuntcloud locked user " -Body " Yuntcloud locked user " -Attachments " C:\get_locked_user\yuntcloud_ Lockeduser.csv " -Credential  $cred  -SmtpServer mail.yuntcloud.com -Encoding  ([ System.text.encoding]::utf8)  start-sleep 3} #Unlock  aaa.yuntcloud.com locked user[ Email protected] () if  ([String]::isnullorempty ($ ($aaa _locKeduser|%{$_.name})) {write-host  "Aaa no locked user" start-sleep 5add-content c :\get_locked_user\locked.txt -value  "Aaa no locked user" #Send-mailmessage -from   $nfrom  -To  $nto  -Subject  "Aaa no locked user"  -Body  "AAA  no locked user " -Credential  $cred  -smtpserver mail.yuntcloud.com - encoding  ([System.text.encoding]::utf8)}else{write-host  "Aaa locked user" foreach ($i   in  $aaa _lockeduser) {write-host  $i. name$lockeduser+= $i}start-sleep 5search-adaccount - lockedout -searchbase  "Dc=aaa,dc=yuntcloud,dc=com"  -server aaadc1 | unlock-adaccountwrite-host  "aaa all locked account were unlocked"  $ lockeduser | export-csv -path c:\get_locked_user\aaa_lockeduser.csv -notypeinformation -encoding  Utf8add-content c:\get_locked_user\locked.txt -value  "Aaa locked user" Add-content c:\get_locked_user\locked.txt  -value $ ($aaa _lockeduser|%{$_.name}) #Send-mailmessage -from  $nfrom  -To  $nto  -Subject  "Aaa locked user"  -Body  "Aaa locked user"  -attachments   "C:\get_locked_user\aaa_lockeduser.csv"  -Credential  $cred  -SmtpServer  mail.yuntcloud.com -encoding  ([System.text.encoding]::utf8)  start-sleep 3} #Unlock   Bbb.yuntcloud.com locked user[email protected] () if  ([String]::isnullorempty ($ ($bbb _ Lockeduser|%{$_.name})) {write-host  "Bbb no locked user" start-sleep 5add-content  C:\get_locked_user\locked.txt -Value  "Bbb no locked user" #Send-mailmessage  -From  $nfrom  -To  $nto  -Subject  "Bbb no locked user"  -body   "Bbb no locked user"  -credential  $cred  -SmtpServer mail.yuntcloud.com -Encoding  ([System.text.encoding]::utf8)} else{write-host  "Bbb locked user" foreach ($i  in  $bbb _lockeduser) {write-host $ i.name$lockeduser+= $i}start-sleep 5search-adaccount -lockedout -searchbase  "dc=bbb,dc= Yuntcloud,dc=com " -server bbbdc1 | unlock-adaccountwrite-host  "bbb all locked account were unlocked"  $ lockeduser | export-csv -path c:\get_locked_user\bbb_lockeduser.csv -notypeinformation -encoding  utf8 add-content c:\get_locked_user\locked.txt -value  "Bbb locked user" add-content c:\get_locked_user\locked.txt -value $ ($bbb _lockeduser|%{$_.name}) #Send-mailmessage  -From  $nfrom  -To  $nto  -Subject  "Bbb locked user"  -Body  "BBB  locked user " -Attachments " C:\get_locked_user\bbb_lockeduser.CSV  -Credential  $cred  -SmtpServer mail.yuntcloud.com -Encoding  ([ System.text.encoding]::utf8)  start-sleep 3} #Unlock  ccc.yuntcloud.com locked user[ Email protected] () if  ([String]::isnullorempty ($ ($CCC _lockeduser|%{$_.name})) {write-host  "CCC  no locked user "Start-sleep 5add-content c:\get_locked_user\locked.txt -value   "Ccc no locked user" #Send-mailmessage -from  $nfrom  -To  $nto  - subject  "Ccc no locked user"  -Body  "Ccc no locked user"  - credential  $cred  -SmtpServer mail.yuntcloud.com -Encoding  ([System.Text.Encoding]:: UTF8)}else{write-host  "Ccc locked user" foreach ($i  in  $ccc _lockeduser) {Write-host   $i. name$lockeduser+= $i}start-sleep 5search-adaccount -lockedout -searchbase  "DC=CCC, Dc=yuntcloud,dc=com " -server cccdc1 |unlock-adaccountwrite-host  "ccc all locked account were unlocked"  $ lockeduser | export-csv -path c:\get_locked_user\ccc_lockeduser.csv -notypeinformation -encoding  utf8 add-content c:\get_locked_user\locked.txt -value  "Ccc locked user" add-content c:\get_locked_user\locked.txt -value $ ($CCC _lockeduser|%{$_.name}) #Send-mailmessage  -From  $nfrom  -To  $nto  -Subject  "Ccc locked user"  -Body  "CCC  locked user " -Attachments " C:\get_locked_user\ccc_lockeduser.csv " -credential $ cred -smtpserver mail.yuntcloud.com -encoding  ([System.text.encoding]::utf8)   START-SLEEP 3} $body =get-content c:\get_locked_user\locked.txt| out-stringif  ((test-path c:\get_locked_user\yuntcloud_lockeduser.csv)  -eq  $true)  - and   ((TEST-PATH C:\GET_LOCKED_USER\AAa_lockeduser.csv)  -eq  $false)  -and  ((test-path c:\get_locked_user\bbb_lockeduser.csv)  -eq  $false)  -and  ((test-path c:\get_locked_user\ccc_lockeduser.csv)  -eq $ False)  {Send-MailMessage -From  $nfrom  -To  $nto  -Subject  "yuntcloud 4  domains all locked user information total " -Body  $body  - attachments  "C:\get_locked_user\yuntcloud_lockeduser.csv"  -Credential  $cred  -smtpserver  mail.yuntcloud.com -Encoding  ([System.text.encoding]::utf8)  }elseif  ((Test-path  c:\get_locked_user\yuntcloud_lockeduser.csv)  -eq  $true)  -and   ((test-path  C:\get_locked_user\aaa_lockeduser.csv)  -eq  $true)  -and  ((Test-path c:\get_locked_user \bbb_lockeduser.csv)  -eq  $false)  -and  ((test-path c:\get_locked_user\ccc_ lockeduser.csv)  -eq  $false)) &nbsp {send-mailmessage -from  $nfrom  -To  $nto  -Subject  "Yuntcloud 4 domains  all locked user information total " -Body  $body  -Attachments " c \ Get_locked_user\yuntcloud_lockeduser.csv "," C:\get_locked_user\aaa_lockeduser.csv " -Credential  $cred  -SmtpServer mail.yuntcloud.com -Encoding  ([System.text.encoding]::utf8)  }elseif   ((test-path c:\get_locked_user\yuntcloud_lockeduser.csv)  -eq  $true)  -and    ((test-path c:\get_locked_user\aaa_lockeduser.csv)  -eq  $true)  -and  ((Test-path  c:\get_locked_user\bbb_lockeduser.csv)  -eq  $true)  -and  ((test-path c:\get_ locked_user\ccc_lockeduser.csv)  -eq  $false))  {Send-MailMessage -From  $nfrom  -to   $nto  -Subject  "yuntcloud 4 domains all locked user information  total " -body&nbSP; $body  -Attachments  "C:\get_locked_user\yuntcloud_lockeduser.csv", "C:\get_locked_user\aaa_ Lockeduser.csv "," C:\get_locked_user\bbb_lockeduser.csv " -Credential  $cred  -SmtpServer  mail.yuntcloud.com -encoding  ([System.text.encoding]::utf8)  }elseif  (((test-path c:\ get_locked_user\yuntcloud_lockeduser.csv)  -eq  $true)  -and   ((test-path c:\get_ locked_user\aaa_lockeduser.csv)  -eq  $true)  -and  ((test-path c:\get_locked_user\bbb_ lockeduser.csv)  -eq  $true)  -and  ((test-path c:\get_locked_user\ccc_lockeduser.csv)  -eq  $true)  {Send-MailMessage -From  $nfrom  -To  $nto  -Subject  " Yuntcloud 4 domains all locked user information total " -Body $ body -attachments  "C:\get_locked_user\yuntcloud_lockeduser.csv", "C:\get_locked_user\aaa_ Lockeduser.csv "," C:\get_locked_user\bbb_lockeduSer.csv "," C:\get_locked_user\ccc_lockeduser.csv " -Credential  $cred  -SmtpServer  mail.yuntcloud.com -encoding  ([System.text.encoding]::utf8)  }else{send-mailmessage -from   $nfrom  -To  $nto  -Subject  "yuntcloud 4 domains all locked  User information total " -Body  $body  -Credential  $cred  -SmtpServer  mail.yuntcloud.com -encoding  ([System.text.encoding]::utf8)  }



This article is from the "Zhou Ping Microsoft Technology Exchange Platform" blog, please be sure to keep this source http://yuntcloud.blog.51cto.com/1173839/1945987

PowerShell Management Series (40) PowerShell query and Unlock ad account (after improvement, only send mail once)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.