AD批量添加群组与用户
一、背景
因管理需要计划将现有的serv-u服务改成ad+iis+ftp+ntfs架构,所以需要在ad中新建对应群组并添加用户。
为提高效率减少重复工作,编写一个批量新增群组及添加用户脚本,以下为测试环境。
二、汇总
- 注意事项:
- 用户必须存在,在制作用户文件时必须先行过滤,否则脚本将中途中断
- 所有群组的用户存在一个用户文件中,其长短不一存在无值(csv文件中间列)或空值(csv文件最后一列),需求将其过滤
- 脚本只添加群组必须属性,附加属性需视需求修改
- 制作csv文件所用的一对多查找函数:
=INDEX(A:A,SMALL(IF($B$2:$B$200="Sam",ROW($2:$200),4^8),ROW(A1)))&""
- 完整脚本
Import-Module ActiveDirectory
$ngroups=Import-Csv C:\Data\ngs.csv
$nusers=Import-Csv C:\Data\nus.csv
foreach ($ngroup in $ngroups) {
#新建组
New-ADGroup -Name $ngroup.name -SamAccountName $ngroup.name -GroupCategory $ngroup.GroupCategory -GroupScope $ngroup.Groupscope -Path $ngroup.path -Description $ngroup.description -PassThru
Get-ADGroup -Identity $ngroup.name | Set-ADGroup -Replace @{info=$ngroup.info}
#新增成员
Add-ADGroupMember -Identity $ngroup.name -Members ($nusers.($ngroup.name) | Where-Object {$_ -ne ''} ) -PassThru
}
- 群组文件ngs.csv
name,path,groupcategory,groupscope,description,info
ftp-ops-w,"OU=FTP,OU=Group,DC=lxy,DC=lin",Security,Global,"ip/ftp/ops/","DRI:xx,TEL:xx"
ftp-ops-r,"OU=FTP,OU=Group,DC=lxy,DC=lin",Security,Global,"ip/ftp/ops/","DRI:xx,TEL:xx"
ftp-dba-w,"OU=FTP,OU=Group,DC=lxy,DC=lin",Security,Global,"ip/ftp/dba/","DRI:xx,TEL:xx"
ftp-dba-r,"OU=FTP,OU=Group,DC=lxy,DC=lin",Security,Global,"ip/ftp/dba/","DRI:xx,TEL:xx"
- 用户文件:nus.csv
ftp-ops-w,ftp-ops-r,ftp-dba-w,ftp-dba-r
user01,user02,user03,user04
user05,,user06,user07
user08,,user09,
三、分解
- 导入AD模块
Import-Module ActiveDirectory
- 新建帐号
foreach ( $num in 1..10 ) {
$user='USER'+$num
New-ADUser $user -Path "OU=Test,DC=iku,DC=lxy" -Enabled:$true -AccountPassword(ConvertTo-SecureString "lxy1989." -AsPlainText -Force)
}
- 筛选离职或不存在帐号
$newusers= Import-Csv .\nu.csv
$newgroups= Import-Csv .\ng.csv
# 新建数组保存离职或不存在帐号
$array_error_user = New-Object -TypeName System.Collections.ArrayList
$array_disabled_user = New-Object -TypeName System.Collections.ArrayList
foreach ($newgroup in $newgroups) {
$newuser=($newusers.($newgroup.name) | Where-Object {$_ -ne ''})
foreach ($user in $newuser) {
$user_abled= (Get-ADUser $user).enabled # 查询帐号是否被禁用,默认情况下只有离职的帐号才会被禁用
$returned=$? # 若帐号不存在,则返回false
if ($returned -eq $true)
{
if ($user_abled -eq $false)
{$array_disabled_user.Add($user+'@'+($newgroup.name))} # 将被禁用(离职)的帐号添加至数组
}
else
{ $array_error_user.add($user+'@'+($newgroup.name)) } # 将不存在的帐号添加至数组
}
}
echo "The following user is disabled :"$array_disabled_user
echo "The following user does not exist :"$array_error_user
- 从用户文件中删除离职或不存在帐号
vi user.error
xx
xxx
xx
:%s/@.*//g
vi deluser.sh
#!/bin/bash
#在sed中引用变量用双引号
for user in $(cat user.error)
do
sed -i "s/$user//g" nu.csv
done
- 导入群组与用户文件
必须确认群组及用户是存在的,否则会导至脚本异常,后续用户用户添加
$ngroups=Import-Csv C:\Data\ngs.csv
$nusers=Import-Csv C:\Data\nus.csv
- 新建群组
备注信息info无命令参数,通过设置hash值添加
# version1
foreach ($ngroup in $ngroups) {
New-ADGroup -Name $ngroup.name -SamAccountName $ngroup.name -GroupCategory $ngroup.GroupCategory -GroupScope $ngroup.Groupscope -Path $ngroup.path -PassThru
Get-ADGroup -Identity $ngroup.name | Set-ADGroup -Replace @{info=$ngroup.info}
}
# version2
# 增加了群组是否存在的判断
$ngroups = Import-Csv D:\PS\NewGroup\201807\ng.csv -Encoding Unicode
foreach ($ngroup in $ngroups) {
$drop = Get-ADGroup $ngroup.name
$return = $?
# 检查组是否存,不存在则新建组
if ($return -eq $false){
New-ADGroup -Name $ngroup.name -SamAccountName $ngroup.name -GroupCategory $ngroup.groupcategory -GroupScope $ngroup.groupscope -Path $ngroup.path -Description $ngroup.description
Get-ADGroup $ngroup.name | Set-ADGroup -Replace @{info=$ngroup.info}
}
}
- 为群组添加用户
用户文件中并非所有群组都有用户,命令遇见异常会中止所有操作,所有需求过滤空数据;
在CSV文件中最后一列被识别为空值,无法使用‘’识别,通过在每列后增加一列逗号解决。
foreach ($ngroup in $ngroups) {
Add-ADGroupMember -Identity $ngroup.name -Members ($nusers.($ngroup.name) | Where-Object {$_ -ne ''} ) -PassThru
}
PS C:\Users\Administrator> ($nusers.'ftp-dba-r' | Where-Object {$_ -ne ''}).count
3
---nu.csv
ftp-ops-w,ftp-ops-r,ftp-dba-w,ftp-dba-r
user1,user2,user3,user4,
user5,,user6,user7,
user8,,user9,,
---
PS C:\> ($nusers.'ftp-dba-r' | Where-Object {$_ -ne ''}).count
2
- 查看群组用户信息
foreach ($ngroup in $ngroups) {
Get-ADGroupMember -Identity $ngroup.name | select @{name='group';expression={$ngroup.name}},@{name='name';expression={$_.name}}
}
group name
----- ----
ftp-ops-w USER1
ftp-ops-w USER5
ftp-ops-w USER8
ftp-ops-r USER2
ftp-dba-w USER3
ftp-dba-w USER6
ftp-dba-w USER9
ftp-dba-r USER4
ftp-dba-r USER7
- 移除群组所有用户
foreach ($ngroup in $ngroups) {
Remove-ADGroupMember -Identity $ngroup.name -Members (Get-ADGroupMember -Identity $ngroup.name)
}
四、更新
在使用过程中发现脚本的功能实现方式生硬,书写格式并不规范,不便阅读,所以作了更新。
- 变量命名不易理解,已改用单词
- 脚本编写时没使用缩进
# $ngroups = Import-Csv D:\PS\NewGroup\201807\ng.csv -Encoding Unicode
# 新建组
<#
foreach ($ngroup in $ngroups) {
$test = Get-ADGroup $ngroup.name
$return = $?
if ($return -eq $false){
New-ADGroup -Name $ngroup.name -SamAccountName $ngroup.name -GroupCategory $ngroup.groupcategory -GroupScope $ngroup.groupscope -Path $ngroup.path -Description $ngroup.description
Get-ADGroup $ngroup.name | Set-ADGroup -Replace @{info=$ngroup.info}
}
}
#>
# 清空组成员
<#
foreach ($ngroup in $ngroups) {
Remove-ADGroupMember -Identity $ngroup.name -Members (Get-ADGroupMember -Identity $ngroup.name)
}
#>
# 查询组成员
<#
foreach ($ngroup in $ngroups) {
Get-ADGroupMember -Identity $ngroup.name | select @{name='group';expression={$ngroup.name}},@{name='name';expression={$_.name}}
}
#>
# $ngroups = Import-Csv D:\PS\NewGroup\201807\ngw.csv
# $nusers = Import-Csv D:\PS\NewGroup\201807\nus.csv
# 添加成员
<#
foreach ($ngroup in $ngroups) {
Add-ADGroupMember -Identity $ngroup.name -Members ($nusers.($ngroup.name) | Where-Object {$_ -ne ''} ) -PassThru -Confirm:$false
}
#>
# 统计各群组用户数
#<
[int]$sum = 0
$re =foreach ($ngroup in $ngroups){
$user_num = (Get-ADGroupMember ($ngroup.name) | Where-Object {$_ -ne ''}).count
$user_num | select @{name='group';ex={$ngroup.name}},@{name='num';ex={$user_num}}
$sum += $user_num
}
echo $re
echo $sum
#>