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

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.