批量创建OU和AD账号
#导出某个OU下的所有OU Get-ADOrganizationalUnit -Filter * -SearchBase "OU=智能中心,OU=Staff,DC=yy,DC=com" -SearchScope Subtree |select DistinguishedName
$ous = gc D:\Operations\Scripts\ou-zhineng.txt $ous.Count #先对OU进行排序,先创建路径最短的OU [array]$objs = $null foreach ($ou in $ous) { $count = $ou.Split(",").length $props=@{'ou'=$ou;'count'=$count} $obj = New-Object -TypeName psobject -Property $props $objs += $obj } $objs_create = $objs |sort count #创建OU foreach ($obj in $objs_create) { $ou = $obj.ou.Replace("DC=yy,DC=com","DC=test19,DC=com") $ou_split = $ou.Split(',') $ou_name = $ou_split[0].Split("=")[1] $ou_name $ou_path = $ou_split[1..100] -join ',' $ou_path New-ADOrganizationalUnit -Name $ou_name -Path $ou_path }
#导出AD用户属性 Get-ADUser -Filter * -Properties Name,Surname,GivenName,DisplayName,Department,City,EmployeeNumber,mobile,MobilePhone,StreetAddress,Title,DistinguishedName -SearchBase "OU=智能中心,OU=Staff,DC=yy,DC=com" |select SamAccountName,Name,Surname,GivenName,DisplayName,Department,City,EmployeeNumber,mobile,MobilePhone,StreetAddress,Title,DistinguishedName |Export-Csv C:\Operations\Scripts\userszhi.csv -Encoding Default -NoTypeInformation #批量创建AD账号 $users = Import-Csv D:\Operations\Scripts\usersit.csv -Encoding Default foreach ($user in $users) { $AccountPassword = "Y20220510" $path = ($user.DistinguishedName.Split(",")[1..100] -join ',').replace("DC=yixin,DC=dk","DC=test19,DC=com") $UserPrincipalName = $user.SamAccountName + "@test19.com" if ($user.Title.Length -gt 0) #某些用户属性字段不全,如果Title为空,则减少字段属性 { New-ADUser -Name $user.Name -Path $Path -samAccountName $user.SamAccountName -UserPrincipalName $UserPrincipalName -Enabled $true -AccountPassword (ConvertTo-SecureString $AccountPassword -AsPlainText -force) -passthru -OtherAttributes @{'title'=$user.Title;'GivenName'=$user.GivenName;'DisplayName'=$user.DisplayName;'Department'=$user.Department;'EmployeeNumber'=$user.EmployeeNumber;'mobile'=$user.mobile;'streetaddress'=$user.streetaddress} Get-ADUser $user.SamAccountName |Set-ADUser -Surname $user.Surname } else { New-ADUser -Name $user.Name -Path $Path -samAccountName $user.SamAccountName -UserPrincipalName $UserPrincipalName -Enabled $true -AccountPassword (ConvertTo-SecureString $AccountPassword -AsPlainText -force) -passthru -OtherAttributes @{'DisplayName'=$user.DisplayName} } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步