当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 将用户添加到组

作者:精品下载站 日期:2024-12-14 22:34:58 浏览:12 分类:玩电脑

使用 PowerShell 将用户添加到组


如何使用 PowerShell 从 CSV 文件将用户批量添加到 AD 安全组?您可以在 AD 中选择多个用户并将其添加到组中,但是如果您的用户分布在不同的 OU 中怎么办?这是使用 PowerShell 自动执行任务的绝佳方法。在本文中,您将了解如何使用 PowerShell 脚本将用户批量添加到组中。

信息

您需要将 CSV 文件中的用户列表添加到安全组该列表使用 UserPrincipalName 属性填充。

完成任务有两种选择:

  • 手动在 Active Directory 用户和计算机中搜索用户,并将其添加到安全组中。如果您的清单很长,这将非常耗时。不仅如此,您还可能错过列表中的某个用户。

  • 使用 PowerShell 自动化搜索,并将用户添加到安全组。花费的时间会更少,并且您不会错过任何用户。

PowerShell 非常适合自动化,这也是我们推荐使用的。

注意:您要将用户添加到多个组吗?请阅读使用 PowerShell 将用户添加到多个组。

检查安全组

如果没有安全组,请创建一个。在此示例中,我们有安全组Pilot。会员部分只有一名会员。

[玩转系统] 使用 PowerShell 将用户添加到组

单击常规选项卡。确保复制组名称(Windows 2000 之前的版本)

[玩转系统] 使用 PowerShell 将用户添加到组

了解更多:通过 PowerShell 列出安全组中的所有用户 »

使用 Import-Csv cmdlet 检查 CSV 文件

检查 CSV 文件并确保您使用了正确的标头。在我们的例子中,它是 CSV 文件 Users.csv 和标题 UserPrincipalName

[玩转系统] 使用 PowerShell 将用户添加到组

重要提示:检查每行后面是否有空格。如果是这样,您将收到错误,并且脚本将无法删除用户。

一个很好的方法是在字段周围添加引号。

[玩转系统] 使用 PowerShell 将用户添加到组

将 CSV 文件放入 C:\Temp 文件夹中。如果没有临时文件夹,请创建一个。

[玩转系统] 使用 PowerShell 将用户添加到组

以管理员身份运行 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 文件中的用户。在本例中,是试点组。

[玩转系统] 使用 PowerShell 将用户添加到组

输出将显示在 Windows PowerShell 控制台中。不仅如此,它还会在日志中显示输出,因为 PS 脚本中添加了脚本。转到 C:\temp 文件夹并打开 Add-ADUsers.log 文件。

[玩转系统] 使用 PowerShell 将用户添加到组

一切看起来都很棒!这是否可以帮助您使用 PowerShell 从 CSV 文件将用户批量添加到安全组?

继续阅读:使用组策略管理 Microsoft Office »

结论

您学习了如何使用 PowerShell 从 CSV 将用户添加到组。下载 Add-ADUsers.ps1 PowerShell 脚本,编辑 CSV 路径和目标组。运行脚本并验证 AD 用户是否已成功添加到组中。 PowerShell 非常适合自动化该过程。

您喜欢这篇文章吗?您可能还喜欢在 Exchange 混合中批量创建 Office 365 邮箱。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯