AD新建用户、组、OU

#新建用户

import-csv d:\AD\Users.csv | Foreach {New-ADUser -Name $_.Name -Path $_.Path -samAccountName $_.SamAccountName -UserPrincipalName $_.Userprincipalname -Enabled $true -AccountPassword (ConvertTo-SecureString $_.AccountPassword -AsPlainText -force) -passthru -PasswordNeverExpires ($_.PasswordNeverExpires -eq "1")}

$Users = import-csv D:\Operations\Scripts\Users.csv -Encoding Default
$DomainName = "uat.com"

Foreach ($User in $Users)
{
 $Userprincipalname = $User.SamAccountName + '@' + $DomainName
 $OU = $User.OU.Replace(';',',')
 New-ADUser -Name $User.Name -Path $OU -samAccountName $User.SamAccountName -UserPrincipalName $Userprincipalname -DisplayName $User.DisplayName -Title $User.Title -Department $User.Department -Enabled $true -AccountPassword (ConvertTo-SecureString $User.AccountPassword -AsPlainText -force) -passthru |Out-Null
}

csv格式:

 

 

#删除用户

get-aduser -filter * -SearchBase "ou=oo,ou=admins,dc=ddv,dc=com" |? {$_.name -eq "test02"} | Remove-ADUser -Confirm:$false

#新建组
import-csv d:\AD\Groups.csv | Foreach {New-ADGroup -Name $_.GroupName -Path $_.Path -GroupScope Global}

#将用户加入组
import-csv d:\AD\Members.csv | Foreach {Add-ADGroupMember -Identity $_.Memberof -Members $_.Users}

#新建OU
import-csv d:\AD\OU.csv | Foreach {New-ADOrganizationalUnit -Name $_.OUName -Path $_.OUPath}

 

#导入组策略
import-csv C:\ADPS\GPO.csv | Foreach {import-gpo -BackupId $_.BackupID -TargetName $_.TargetName -path "C:\ADPS\GPO\" -CreateIfNeeded}
#链接组策略
import-csv C:\ADPS\GPLink.csv | Foreach {New-GPLink -Name $_.TargetName -Target $_.OU -LinkEnabled Yes}

 

新建寻呼机字段(pager):

Set-ADUser -Credential $cred $AccountName -replace @{pager=$AccountNumber}

批量创建OU:

$ous = gc D:\Operations\Scripts\ous.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
$ou_split = $ou.Split(',')
#获取要创建的OU名称、上一级OU路径
$ou_name = $ou_split[0].Split("=")[1]
$ou_path = $ou_split[1..100] -join ','
#检查上一级OU路径是否存在,不存在则无法创建OU
if ( Get-ADOrganizationalUnit -Filter "distinguishedName -eq '$ou_path'" )
    { New-ADOrganizationalUnit -Name $ou_name -Path $ou_path }
else { Write-Host "$ou_path 上级OU不存在,无法创建OU $ou" }


}

ous格式:

 

posted on 2013-12-24 14:57  momingliu11  阅读(3544)  评论(0编辑  收藏  举报