批量创建OU和AD账号

网友投稿 670 2022-09-12

批量创建OU和AD账号

批量创建OU和AD账号

#导出某个OU下的所有OUGet-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 = $nullforeach ($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 #创建OUforeach ($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_pathNew-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 Defaultforeach ($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} }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Windows 10 系统垃圾清理
下一篇:我的电脑防护安全软件
相关文章

 发表评论

暂时没有评论,来抢沙发吧~