PowerShell Management Series (39) PowerShell query and unlock ad account

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 every morning to send mail to point to the mailbox.

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  "Djds123"  -asplaintext –force$cred = new-object  system.management.automation. Pscredential ($UserName,  $Password) $nFrom  =  "[email protected]" $nTo  =  "[Email  protected] "$lockeduser  = @ () #Unlock  yuntcloud Locked userif  ([string]::i Snullorempty ($ ($yuntcloud _lockeduser|%{$_.name})) { write-host  "yuntcloud no locked  User  Start-Sleep 5 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 5 Search-ADAccount -LockedOut -SearchBase  "dc= Yuntcloud,dc=com " -SERVER DC01&Nbsp;| unlock-adaccount write-host  "yuntcloud all locked account were  Unlocked "  $lockeduser  | export-csv -path c:\yuntcloud_lockeduser.csv - notypeinformation -encoding utf8 send-mailmessage -from  $nfrom  -To  $nto  -Subject  "Yuntcloud locked user"  -Body  "Yuntcloud locked user"  - attachments  "C:\yuntcloud_lockeduser.csv"  -Credential  $cred  -SmtpServer  mail.yuntcloud.com -encoding  ([System.text.encoding]::utf8)  start-sleep 3} #Unlock   aaa.yuntcloud.com locked user$lockeduser = @ () if  ([String]::isnullorempty ($ ($aaa _ Lockeduser|%{$_.name})) { write-host  "Aaa no locked user"  Start-Sleep 5  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 5 search-adaccount -lockedout -searchbase  "dc=aaa,dc= Yuntcloud,dc=com " -server aaaDC1 | Unlock-ADAccount Write-Host " Aaa all  locked account were unlocked "  $lockeduser  | export-csv -path c:\ aaa_lockeduser.csv -notypeinformation -encoding utf8 send-mailmessage -from $ nfrom -to  $nto  -Subject  "Aaa locked user"  -Body  "aaa locked  User  -Attachments  c:\aaa_lockeduser.csv  -Credential  $cred  -SmtpServer  mail.yuntcloud.com -encoding  ([System.text.encoding]::utf8)  start-sleep 3} #Unlock  bbb.yuntcloud.com locked user$ lockeduser = @ () if  ([String]::isnullorempty ($ ($bbb _lockeduser|%{$_.name}))) { Write-Host   "Bbb no locked user"  Start-Sleep 5 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 5 search-adaccount -lockedout -searchbase  "dc=bbb,dc= Yuntcloud,dc=com " -server bbbdc1 | Unlock-ADAccount Write-Host " Bbb all  locked account were unlocked "  $lockeduser  | export-csv -path c:\bbb_lockeduser.csv - notypeinformation -encoding utf8 send-mailmessage -from  $nfrom  -To  $nto  -Subject  "Bbb locked user"  -Body  "Bbb locked user"  -attachments   "C:\bbb_lockeduser.csv"  -Credential  $cred  -smtpserver mail.yuntcloud.com - encoding  ([System.text.encoding]::utf8)  start-sleep 3} #Unlock  ccc.yuntcloud.com  locked user$lockeduser = @ () if  ([String]::isnullorempty ($ ($CCC _lockeduser|%{$_.name}))) {  Write-Host  "Ccc no locked user"  start-sleep 5 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 5 search-adaccount -lockedout -searchbase  "Dc=ccc,dc=yuntcloud,dc=com"  -server cccDC1 | Unlock-ADAccount Write-Host  "ccc all locked  account were unlocked "  $lockeduser  | export-csv -path c:\ccc_lockeduser.csv  -NoTypeInformation -Encoding utf8 Send-MailMessage -From  $nfrom  -to $ nto -subject  "Ccc locked user"  -Body  "Ccc locked user"  - attachments  "C:\ccc_lockeduser.csv"  -Credential  $cred  -smtpserver mail.yuntcloud.com  -Encoding  ([System.text.encoding]::utf8)  start-sleep 3}


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/1945726

PowerShell Management Series (39) PowerShell query and unlock ad account

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.