[玩转系统] 使用 PowerShell 将用户添加到组
作者:精品下载站 日期:2024-12-14 22:34:58 浏览:12 分类:玩电脑
使用 PowerShell 将用户添加到组
如何使用 PowerShell 从 CSV 文件将用户批量添加到 AD 安全组?您可以在 AD 中选择多个用户并将其添加到组中,但是如果您的用户分布在不同的 OU 中怎么办?这是使用 PowerShell 自动执行任务的绝佳方法。在本文中,您将了解如何使用 PowerShell 脚本将用户批量添加到组中。
信息
您需要将 CSV 文件中的用户列表添加到安全组。该列表使用 UserPrincipalName 属性填充。
完成任务有两种选择:
手动在 Active Directory 用户和计算机中搜索用户,并将其添加到安全组中。如果您的清单很长,这将非常耗时。不仅如此,您还可能错过列表中的某个用户。
使用 PowerShell 自动化搜索,并将用户添加到安全组。花费的时间会更少,并且您不会错过任何用户。
PowerShell 非常适合自动化,这也是我们推荐使用的。
注意:您要将用户添加到多个组吗?请阅读使用 PowerShell 将用户添加到多个组。
检查安全组
如果没有安全组,请创建一个。在此示例中,我们有安全组Pilot。会员部分只有一名会员。
单击常规选项卡。确保复制组名称(Windows 2000 之前的版本)。
了解更多:通过 PowerShell 列出安全组中的所有用户 »
使用 Import-Csv cmdlet 检查 CSV 文件
检查 CSV 文件并确保您使用了正确的标头。在我们的例子中,它是 CSV 文件 Users.csv 和标题 UserPrincipalName。
重要提示:检查每行后面是否有空格。如果是这样,您将收到错误,并且脚本将无法删除用户。
一个很好的方法是在字段周围添加引号。
将 CSV 文件放入 C:\Temp 文件夹中。如果没有临时文件夹,请创建一个。
以管理员身份运行 Windows PowerShell。确保 PowerShell 可以读取该文件,运行 Import-Csv cmdlet。
PS C:\> Import-Csv "C:\Temp\Users.csv"
UserPrincipalName
-----------------
Amanda.Morgan@exoip.com
Max.Fraser@exoip.com
Piers.Bower@exoip.com
Kylie.Davidson@exoip.com
Richard.Grant@exoip.com
Boris.Campbell@exoip.com
Nicholas.Murray@exoip.com
Leonard.Clark@exoip.com
Ruth.Dickens@exoip.com
Jonathan.Fisher@exoip.com
Grace.Rees@exoip.com
Patrick.Mors@exoip.com
John.Maverick@exoip.com
Ali.Tajran@exoip.com
Alysia.Maverick@exoip.com
Mohammad.Fistak@exoip.com
继续阅读:导入 CSV 分隔符 PowerShell »
将用户添加到组 PowerShell 脚本
下载 Add-ADUsers.ps1 PowerShell 脚本或将以下代码复制并粘贴到记事本中。将其命名为 Add-ADUsers.ps1 并将其放置在 C:\scripts 文件夹中。如果没有,请创建一个 scripts 文件夹。
# Start transcript
Start-Transcript -Path C:\Temp\Add-ADUsers.log -Append
# Import AD Module
Import-Module ActiveDirectory
# Import the data from CSV file and assign it to variable
$Users = Import-Csv "C:\Temp\Users.csv"
# Specify target group name (pre-Windows 2000) where the users will be added to
# You can add the distinguishedName of the group. For example: CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local
$Group = "Pilot"
foreach ($User in $Users) {
# Retrieve UPN
$UPN = $User.UserPrincipalName
# Retrieve UPN related SamAccountName
$ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$UPN'" | Select-Object SamAccountName
# User from CSV not in AD
if ($ADUser -eq $null) {
Write-Host "$UPN does not exist in AD" -ForegroundColor Red
}
else {
# Retrieve AD user group membership
$ExistingGroups = Get-ADPrincipalGroupMembership $ADUser.SamAccountName | Select-Object Name
# User already member of group
if ($ExistingGroups.Name -eq $Group) {
Write-Host "$UPN already exists in $Group" -ForeGroundColor Yellow
}
else {
# Add user to group
Add-ADGroupMember -Identity $Group -Members $ADUser.SamAccountName -WhatIf
Write-Host "Added $UPN to $Group" -ForeGroundColor Green
}
}
}
Stop-Transcript
第 8 行:编辑 CSV 文件名和路径。
第 12 行:编辑目标组。
在下一步中,我们将查看批量添加 AD 用户 PowerShell 脚本。
从 CSV 文件批量添加用户到组
以管理员身份运行 Windows PowerShell。更改 scripts 文件夹的路径并运行 Add-ADUsers.ps1 PowerShell 脚本以将 AD 用户批量添加到组中。
该脚本将遍历 CSV 文件中的所有用户。 -WhatIf 参数添加到脚本的第 35 行 中。如果运行该脚本,环境中不会发生任何事情。相反,您将得到一个显示将发生什么的输出。
Add-ADUsers.ps1 脚本将显示:
如果用户被添加到组中
如果该用户已存在于组中
如果 CSV 文件中的用户在 Active Directory 中不存在
PS C:\> cd c:\scripts
PS C:\scripts> .\Add-ADUsers.ps1
Transcript started, output file is C:\Temp\Add-ADUsers.log
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Amanda.Morgan@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Max.Fraser@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Piers.Bower@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Kylie.Davidson@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Richard.Grant@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Boris.Campbell@exoip.com to Pilot
Nicholas.Murray@exoip.com does not exist in AD
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Leonard.Clark@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Ruth.Dickens@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Jonathan.Fisher@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Grace.Rees@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Patrick.Mors@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added John.Maverick@exoip.com to Pilot
Ali.Tajran@exoip.com already exists in Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Alysia.Maverick@exoip.com to Pilot
What if: Performing the operation "Set" on target "CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local".
Added Mohammad.Fistak@exoip.com to Pilot
Transcript stopped, output file is C:\Temp\Add-ADUsers.log
从 PowerShell 脚本中删除 -WhatIf 参数并重新运行该脚本。 CSV 中的用户将添加到组中。
PS C:\scripts> .\Add-ADUsers.ps1
Transcript started, output file is C:\Temp\Add-ADUsers.log
Added Amanda.Morgan@exoip.com to Pilot
Added Max.Fraser@exoip.com to Pilot
Added Piers.Bower@exoip.com to Pilot
Added Kylie.Davidson@exoip.com to Pilot
Added Richard.Grant@exoip.com to Pilot
Added Boris.Campbell@exoip.com to Pilot
Nicholas.Murray@exoip.com does not exist in AD
Added Leonard.Clark@exoip.com to Pilot
Added Ruth.Dickens@exoip.com to Pilot
Added Jonathan.Fisher@exoip.com to Pilot
Added Grace.Rees@exoip.com to Pilot
Added Patrick.Mors@exoip.com to Pilot
Added John.Maverick@exoip.com to Pilot
Ali.Tajran@exoip.com already exists in Pilot
Added Alysia.Maverick@exoip.com to Pilot
Added Mohammad.Fistak@exoip.com to Pilot
Transcript stopped, output file is C:\Temp\Add-ADUsers.log
验证添加用户的安全组
脚本完成后,查看 Active Directory 用户和计算机。转到安全组并验证您是否看到成员选项卡中 CSV 文件中的用户。在本例中,是试点组。
输出将显示在 Windows PowerShell 控制台中。不仅如此,它还会在日志中显示输出,因为 PS 脚本中添加了脚本。转到 C:\temp 文件夹并打开 Add-ADUsers.log 文件。
一切看起来都很棒!这是否可以帮助您使用 PowerShell 从 CSV 文件将用户批量添加到安全组?
继续阅读:使用组策略管理 Microsoft Office »
结论
您学习了如何使用 PowerShell 从 CSV 将用户添加到组。下载 Add-ADUsers.ps1 PowerShell 脚本,编辑 CSV 路径和目标组。运行脚本并验证 AD 用户是否已成功添加到组中。 PowerShell 非常适合自动化该过程。
您喜欢这篇文章吗?您可能还喜欢在 Exchange 混合中批量创建 Office 365 邮箱。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag