使用 PowerShell 管理安全组

此文章适用于 Microsoft 365 企业版和 Office 365 企业版。

可以使用适用于 Microsoft 365 的 PowerShell 作为管理安全组Microsoft 365 管理中心的替代方法。

本文介绍列出、创建、更改设置和删除安全组。

如果本文中的命令块要求指定变量值,请使用以下步骤。

  1. 将命令块复制到剪贴板,并将其粘贴到记事本或 PowerShell 集成脚本环境 (ISE) 。
  2. 填写变量值并删除“”<和“>”字符。
  3. 在 PowerShell 窗口或 PowerShell ISE 中运行命令。

请参阅 维护安全组成员身份 以使用 PowerShell 管理组成员身份。

使用 Microsoft Graph PowerShell 管理安全组

注意

Azure Active Directory 模块将替换为 Microsoft Graph PowerShell SDK。 可以使用 Microsoft Graph PowerShell SDK 访问所有 Microsoft Graph API。 有关详细信息,请参阅 Microsoft Graph PowerShell SDK 入门

首先, 连接到 Microsoft 365 租户

管理安全组需要 Group.ReadWrite.All 权限范围或“列出 subscribedSkus”图形 API引用页中列出的其他权限之一。 本文中的某些命令可能需要不同的权限范围,在这种情况下,相关部分将对此进行说明。

Connect-Graph -Scopes Group.ReadWrite.All

列出组

使用此命令列出所有组。

Get-MgGroup -All

使用这些命令可按特定组的显示名称显示其设置。

$groupName="<display name of the group>"
Get-MgGroup -All | Where-Object { $_.DisplayName -eq $groupName }

创建新组

使用此命令创建新的安全组。

Connect-MgGraph -Scopes "Group.Create"
New-MgGroup -Description "<group purpose>" -DisplayName "<name>" -MailEnabled:$false -SecurityEnabled -MailNickname "<email name>"

显示组的设置

使用以下命令显示组的设置。

$groupName="<display name of the group>"
Get-MgGroup -All | Where-Object { $_.DisplayName -eq $groupName } | Select-Object *

删除安全组

使用以下命令删除安全组。

$groupName="<display name of the group>"
$group = Get-MgGroup -Filter "displayName eq '$groupName'"
Remove-MgGroup -GroupId $group.Id

管理安全组的所有者

使用以下命令可显示安全组的当前所有者。

$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "GroupMember.Read.All"

# Display group owners
Get-MgGroupOwner -GroupId (Get-MgGroup | Where-Object { $_.DisplayName -eq $groupName }).Id

使用这些命令将用户帐户的用户 主体名称 (UPN) 添加到安全组的当前所有者。

$userUPN="<UPN of the user account to add>"
$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "User.ReadBasic.All"

# Get the group and user
$group = Get-MgGroup -Filter "displayName eq '$groupName'"
$userId = (Get-MgUser -Filter "userPrincipalName eq '$userUPN'").Id

# Add the user as an owner to the group
$newGroupOwner =@{
  "@odata.id"= "https://graph.microsoft.com/v1.0/users/$userId"
  }

New-MgGroupOwnerByRef -GroupId $group.Id -BodyParameter $newGroupOwner

使用这些命令将用户帐户的 显示名称 添加到安全组的当前所有者。

$userName="<Display name of the user account to add>"
$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "Directory.Read.All", "User.ReadBasic.All"

# Get the group and user
$group = Get-MgGroup -All | Where-Object { $_.DisplayName -eq $groupName }
$userId = (Get-MgUser -All | Where-Object { $_.DisplayName -eq $userName }).Id

# Add the user as an owner to the group
$newGroupOwner =@{
  "@odata.id"= "https://graph.microsoft.com/v1.0/users/$userId"
  }

New-MgGroupOwnerByRef -GroupId $group.Id -BodyParameter $newGroupOwner

使用这些命令,可以通过 UPN 从安全组的当前所有者中删除用户帐户。

$userUPN="<UPN of the user account to remove>"
$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "Directory.ReadWrite.All"

# Get the group and user
$group = Get-MgGroup -Filter "displayName eq '$groupName'" | Select-Object -First 1
$user = Get-MgUser -Filter "userPrincipalName eq '$userUPN'" | Select-Object -First 1

# Remove the user from the group
Remove-MgGroupOwnerByRef -GroupId $group.Id -DirectoryObjectId $user.Id

使用这些命令可从安全组的当前所有者中删除用户帐户的 显示名称

$userName="<Display name of the user account to remove>"
$groupName="<display name of the group>"
$group = Get-MgGroup | Where-Object { $_.DisplayName -eq $groupName }
$user = Get-MgUser | Where-Object { $_.DisplayName -eq $userName }

Remove-MgGroupOwnerByRef -GroupId $group.Id -DirectoryObjectId $user.Id

另请参阅

使用 PowerShell 管理 Microsoft 365 用户帐户、许可证和组

使用 PowerShell 管理 Microsoft 365

PowerShell for Microsoft 365 入门