最近公司收購新工廠需要大批量創建AD賬號,由於AD的自定義屬性比較多需要添加proxyaddress郵件主SMP地址、直線經理Manager屬性、extenstionAttribute4屬性
userid UPN Initials Fistname Lastname DisplayName Office telephoneNumber postalcode fax Email StreetAddress City Province country company C mobile Title Department Manager SMTP
astest35ad [email protected] xx test35 AD test35 ad 506F test xx xxxx [email protected] No. 55, Persiaran Selangor, Seksyen 49 GZ GD China Guangzhou XX Co., Ltd. cn test test xx linemanagerAD SMTP:[email protected]
astest36ad [email protected] xx test36 AD test36 ad 507F test xx xxxx [email protected] No. 55, Persiaran Selangor, Seksyen 50 GZ GD China Guangzhou XX Co., Ltd. cn test test xx linemanagerAD SMTP:[email protected]
astest37ad [email protected] xx test37 AD test37 ad 508F test xx xxxx [email protected] No. 55, Persiaran Selangor, Seksyen 51 GZ GD China Guangzhou XX Co., Ltd. cn test test xx linemanagerAD SMTP:[email protected]
astest38ad [email protected] xx test38 AD test38 ad 509F test xx xxxx [email protected] No. 55, Persiaran Selangor, Seksyen 52 GZ GD China Guangzhou XX Co., Ltd. cn test test xx linemanagerAD SMTP:[email protected]
- PS1代碼如下:
PS C:\Users\xxx> $imports = Import-Csv -path 'C:\Users\xxx\Documents\Windows Server\Power Shell\
Bach AD PS\BulkAD_Final.csv' #導入CSV表格
PS C:\Users\xxx> foreach ($import in $imports) {
$Path = "OU=xx5F,OU=xxx,OU=UserAcc,DC=CBACorp,DC=xxx,DC=asia" $Password = "xxxX123" #自行修改密碼 $enabled = $True $changePW = $False #取消UserMustChangePasswordAtNextLogon,False取消,Ture勾選 $sam = $import.userid $upn = $import.upn $ini = $import.Initials $givenname = $import.fistname $surname = $import.lastname $displayname = $import.displayname $OfficeName = $import.office $telephoneNumber = $import.TelephoneNumber $email = $import.email $StreetAddress = $import.StreetAddress $L = $import.city $st = $import.province $co = $import.country $c = $import.c $mobile = $import.mobile $title = $import.title $department = $import.department $Company = $import.Company
#獲取表格manager列的成員OU對象
$Manager = Get-ADUser -Identity $import.manager -Properties * |Select-Object DistinguishedName
$employeeID = $import.employeeID
$name = $givenname + " " + $ini + " " + $surname
$postalCode = $import.postalCode
$fax = $import.fax
#定義自定義屬性
$custom = @{} #創建空的哈希表
$custom.proxyAddresses = $import.SMTP #給自定義屬性賦值
$custom.extensionAttribute4 = 'xxx' #給自定義屬性賦值
New-ADUser -name $name -SamAccountName $sam -UserPrincipalName $upn -DisplayName $displayname -GivenName $given
name -SurName $surname -Initials $ini -Office $OfficeName -OfficePhone $telephoneNumber -EmailAddress $email -StreetAddr
ess $StreetAddress -City $L -State $st -Country $c -MobilePhone $mobile -Title $title -Department $department -Company $
company -EmployeeNumber $employeeID -postalCode $postalCode -fax $fax -Path $path -AccountPassword (ConvertTo-SecureStri
ng $password -AsPlainText -force) -Enabled $enabled -ChangePasswordAtLogon $changePW -manager $manager -PassThru|#添加attribute自定義屬性 -PassThrul後面可以用管道返回set-aduser命令
set-ADuser -add $custom
}
PS C:\Users\xxx>