2013年6月13日 星期四

使用 Powershell 大量建立AD帳號

 

 

第一步 在 Exchange Powershell 建立Mailuser帳號 ( 若要建立Mailbox user ,把 new-mailuser改成 new-mailbox  )

Import-CSV "C:\temp\1.csv" | foreach {New-Mailuser -Name $_.cn -ExternalEmailAddress $_.ProxyAddress -Alias $_.cn -DisplayName $_.DisplayName -OrganizationalUnit $_.OUpath }

 

1.csv 格式如下:

1 

 

第二步 更改AD帳號顯示資訊

Import-Module activedirectory

執行 aduser.ps1  ( $Fields裡面的欄位,可以查閱 LDAP ,添加所需欄位 )

$Users = Import-Csv "c:\temp\aduser.csv"
$Fields = @{
name = "name"
DisplayName = "DisplayName"
sn = "sn"
givenName = "givenName"
description = "description"
physicalDeliveryOfficeName = "physicalDeliveryOfficeName"
Company = "Company"
telephoneNumber = "telephoneNumber"
otherFacsimileTelephoneNumber = "otherFacsimileTelephoneNumber"
otherIpPhone ="otherIpPhone"
}
ForEach ($User in $Users)
{ $Splat = @{}
ForEach ($Field in ($Fields.Keys))
{ $Key = $Fields[$Field]
If ($User.$Key)
{ $Splat.Add($Field,$User.$Key)
}
}
Set-ADUser $User.SamAccountName -Replace $Splat
}

 

ADUser.csv格式如下:

2

 

第三步 更改AD帳號名稱  --將建立完成帳號的LDAP路徑撈出


Import-CSV "C:\temp\2.csv" | foreach {Rename-ADObject -Identity $_.oupath –NewName $_.name }

2.csv格式如下:

3

 

第四步  更改AD帳號密碼   -- logon這個OU下面的帳號,全都變更密碼為 Ab-123456


Get-ADUser -filter * -SearchBase 'OU=logon,DC=xxx,DC=com' | Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText  "Ab-123456" -Force)


Get-ADUser -filter * -SearchBase 'OU=logon,DC=xxx,DC=com' | Set-ADUser -ChangePasswordAtLogon $false