powershell 之AD管理

1.批量建立OU
建立模板,保存为csv格式,同时用文件编辑器另存UTF-8格式,测试是否无乱码
模板格式:
powershell 之AD管理_第1张图片
查看模板中的中文是否为乱码

Import-Csv "c:\ou.csv" | gm

powershell 之AD管理_第2张图片
批量新建OU代码

Import-Csv "c:\ou.csv" | ForEach-Object{New-ADOrganizationalUnit -Name $_.ouname -Path $_.oupath }

2.检查并批量加入某一指定组

$user = Get-ADUser -Filter * -SearchBase "OU=测试,dc=test,dc=com"
$group = "test"
$groupmember = get-adgroupmember -Identity $group
$xianshi = $user | select name | ft
ForEach-Object {
  if ( "$groupmember" -notcontains "$user" ) 
    {
       Add-ADGroupMember -Identity $group -Members $user
       Write-Output $xianshi 已经加入$group}
else
    {
       Write-Output   $xianshi 已经是$group}
}

3.批量修改AD用户信息

$UserList=Import-Csv C:\Users\Administrator\Desktop\usermax.csv
FOREACH ($Person in $UserList) {
   $Name = $Person.Name
   $Department = $Person.deparment
   $Description = $Person.Description
   $Title = $Person.Title
   Get-ADUser -Identity $Name | Set-ADUser -Department $Department  -Description $Description -Title $Title 
}

4.将指定用户移动到指定OU

csv文件建模格式
powershell 之AD管理_第3张图片

$UserList=Import-Csv C:\Users\Administrator\Desktop\usermax.csv
ForEach ($Person in $UserList) {

Get-ADUser $Person.name | Move-ADObject -TargetPath "ou=IT,ou=合肥,dc=test,dc=com"

}

你可能感兴趣的:(windows)