According to the company's needs, wrote a new ad account, mailbox, add a distribution group PowerShell script, first need to edit the CSV file, the format is as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/F8/wKioL1XxN9GBm92aAACgFOM93kA712.jpg "title=" 1.jpg " alt= "Wkiol1xxn9gbm92aaacgfom93ka712.jpg"/>
The code is as follows, added to the group delay of 6 seconds to run, failed to reproduce the attempt 4 times after the function:
import-module activedirectory$erroractionpreference = "Silentlycontinue" #判断是否有重名账号存在 $f = $FalseImport-csv -path d:\newuser\info.csv | foreach { $a = Get-aduser -identity $_.accountif ($a -ne $null) {echo ($_.account+ ' account already exists in AD. ') $f = $true}else{ } $a = $null}if ($f -eq $true) {echo Please modify the above username in the CSV file. Rerun the script. cmd /c pauseexit}else{echo does not have the name of the username, loading the Exchange module and creating a new user account, please wait ... } #新建账号和邮箱 $erroractionpreference = "Continue" add-pssnapin Microsoft.exchange.management.powershell.e2010$password = convertto-securestring -asplaintext 6yhn^YHN -ForceImport-Csv -Path D:\newuser\Info.csv | foreach { New-Mailbox -Name $_.name -Alias $_.account -OrganizationalUnit ' teset.com /teset/newuser ' -UserPrincipalName ($_.account+ ' @teset. com ') -samaccountname $_.account -displayname $_.name -password $password -Database ' User04 ' -ResetPasswordOnNextLogon $true} $erroractionpreference = "Silentlycontinue" #添加到相应的组choice /t 6 /d y /n | out-nullimport-csv -path d:\newuser\info.csv | foreach {$b = 1$b = add-adprincipalgroupmembership -identity $_.account -memberof shofficeif ($b -eq $null) {echo ($_.account+ ' join Shoffice Group success ')}else{$b = add-adprincipalgroupmembership - Identity $_.account -memberof shofficeif ($b -eq $null) {echo ($_.account+ ' Join Shoffice Group Success ')}else{$b = add-adprincipalgroupmembership -identity $_.account - Memberof shofficeif ($b -eq $null) {echo ($_.account+ ' join Shoffice Group success ')}else{$b = Add-adprincipalgroupmembership -identity $_.account -memberof shofficeif ($b -eq $null) {echo ($_.account+ ' join Shoffice Group Success ')}else{echo ($_.account+ ' join Shoffice Group failed!!) ')}}}} $c = 2$c = add-adprincipalgroupmembership -identity $_.account - Memberof $_.group1if ($c -eq $null) {echo ($_.account+ ' join ' +$_.group1+ ' group success ')}else{$c = add-adprincipalgroupmembership -identity $_.account -memberof $_.group1if ($c - eq $null) {echo ($_.account+ ' join ' +$_.group1+ ' group success ')}else{$c = add-adprincipalgroupmembership -identity $_.account -memberof $_.group1if ($c -eq $null) {echo ($_.account+ ' Join ' +$_.group1+ ' group success ')}else{$c = add-adprincipalgroupmembership -identity $_.account - Memberof $_.group1if ($c -eq $null) {echo ($_.account+ ' join ' +$_.group1+ ' group Success ')}else{echo ( $_.account+ ' join ' +$_.group1+ ' group failed! ')}}} cmd /c "Pause"
After successful execution:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/FC/wKiom1XxOB2h--yfAAFDmt_vRHI305.jpg "title=" 2.jpg " alt= "Wkiom1xxob2h--yfaafdmt_vrhi305.jpg"/>
Have a question, give me a message, discuss together.
This article is from the "Wings of God" blog, so be sure to keep this source http://lvht6.blog.51cto.com/763105/1693466
Create a new ad account, mailbox, and PowerShell script to add a distribution group