用於群組管理的 Microsoft Entra 第 2 版 Cmdlet
本文包含如何使用 PowerShell 在 Microsoft Entra 識別碼中管理群組的範例,這是 Microsoft Entra 的一部分。 它也會告訴您如何設定 Microsoft Graph PowerShell 模組。 首先,您必須 下載 Microsoft Graph PowerShell 模組 。
安裝 Microsoft Graph PowerShell 模組
若要安裝 MgGroup PowerShell 模組,請使用下列命令:
PS C:\Windows\system32> Install-module Microsoft.Graph
若要確認模組已準備好使用,請使用下列命令:
PS C:\Windows\system32> Get-Module -Name "*graph*"
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 1.27.0 Microsoft.Graph.Authentication {Add-MgEnvironment, Connect-MgGraph, Disconnect-MgGraph, Get-MgContext…}
Script 1.27.0 Microsoft.Graph.Groups {Add-MgGroupDriveListContentTypeCopy, Add-MgGroupDriveListContentTypeCopyF…
現在,您可以開始使用模組中的 Cmdlet。 如需 Microsoft Graph 模組中 Cmdlet 的完整描述,請參閱 Microsoft Graph PowerShell 的線上參考檔 。
連線至目錄
您必須先將 PowerShell 會話連線到您想要管理的目錄,才能開始使用 Microsoft Graph PowerShell Cmdlet 來管理群組。 使用下列命令:
PS C:\Windows\system32> Connect-MgGraph -Scopes "Group.ReadWrite.All"
Cmdlet 會提示您輸入要用來存取目錄的認證。 在此範例中,我們使用 karen@drumkit.onmicrosoft.com 來存取示範目錄。 Cmdlet 會傳回確認,以顯示會話已成功連線到您的目錄:
Welcome To Microsoft Graph!
現在,您可以使用 MgGraph Cmdlet 來管理目錄中的群組。
擷取
若要從您的目錄中擷取現有的群組,請使用 Get-MgGroups Cmdlet。
若要擷取目錄中的所有群組,請使用不含參數的 Cmdlet:
PS C:\Windows\system32> Get-MgGroup -All
Cmdlet 會傳回連線目錄中的所有群組。
您可以使用 -GroupId 參數來擷取指定群組 objectID 的特定群組:
PS C:\Windows\system32> Get-MgGroup -GroupId 5e3eba05-6c2b-4555-9909-c08e997aab18 | fl
Cmdlet 現在會傳回 objectID 符合您所輸入參數值的群組:
AcceptedSenders :
AllowExternalSenders :
AppRoleAssignments :
AssignedLabels :
AssignedLicenses :
AutoSubscribeNewMembers :
Calendar : Microsoft.Graph.PowerShell.Models.MicrosoftGraphCalendar
CalendarView :
Classification :
Conversations :
CreatedDateTime : 14-07-2023 14:25:49
CreatedOnBehalfOf : Microsoft.Graph.PowerShell.Models.MicrosoftGraphDirectoryObject
DeletedDateTime :
Description : Sales and Marketing
DisplayName : Sales and Marketing
Id : f76cbbb8-0581-4e01-a0d4-133d3ce9197f
IsArchived :
IsAssignableToRole :
IsSubscribedByMail :
LicenseProcessingState : Microsoft.Graph.PowerShell.Models.MicrosoftGraphLicenseProcessingState
Mail : SalesAndMarketing@M365x64647001.onmicrosoft.com
MailEnabled : True
MailNickname : SalesAndMarketing
RejectedSenders :
RenewedDateTime : 14-07-2023 14:25:49
SecurityEnabled : True
您可以使用 -filter 參數來搜尋特定群組。 此參數會採用 ODATA 篩選子句,並傳回符合篩選的所有群組,如下列範例所示:
PS C:\Windows\system32> Get-MgGroup -Filter "DisplayName eq 'Intune Administrators'"
DeletionTimeStamp :
ObjectId : 31f1ff6c-d48c-4f8a-b2e1-abca7fd399df
ObjectType : Group
Description : Intune Administrators
DirSyncEnabled :
DisplayName : Intune Administrators
LastDirSyncTime :
Mail :
MailEnabled : False
MailNickName : 4dd067a0-6515-4f23-968a-cc2ffc2eff5c
OnPremisesSecurityIdentifier :
ProvisioningErrors : {}
ProxyAddresses : {}
SecurityEnabled : True
注意
MgGroup PowerShell Cmdlet 會實作 OData 查詢標準。 如需詳細資訊,請參閱 使用 OData 端點 在 OData 系統查詢選項中 $filter 。
建立群組
若要在目錄中建立新的群組,請使用 New-MgGroup Cmdlet。 此 Cmdlet 會建立名為 「Marketing」 的新安全性群組:
$param = @{
description="My Demo Group"
displayName="DemoGroup"
mailEnabled=$false
securityEnabled=$true
mailNickname="Demo"
}
New-MgGroup @param
更新群組
若要更新現有的群組,請使用 Update-MgGroup Cmdlet。 在此範例中,我們會變更 「Intune 管理員istrators」 群組的 DisplayName 屬性。 首先,我們會使用 Get-MgGroup Cmdlet 來尋找群組,並使用 DisplayName 屬性進行篩選:
PS C:\Windows\system32> Get-MgGroup -Filter "DisplayName eq 'Intune Administrators'"
DeletionTimeStamp :
ObjectId : 31f1ff6c-d48c-4f8a-b2e1-abca7fd399df
ObjectType : Group
Description : Intune Administrators
DirSyncEnabled :
DisplayName : Intune Administrators
LastDirSyncTime :
Mail :
MailEnabled : False
MailNickName : 4dd067a0-6515-4f23-968a-cc2ffc2eff5c
OnPremisesSecurityIdentifier :
ProvisioningErrors : {}
ProxyAddresses : {}
SecurityEnabled : True
接下來,我們將 Description 屬性變更為新的值 「Intune 裝置管理員istrators」:
PS C:\Windows\system32> Update-MgGroup -GroupId 958d212c-14b0-43d0-a052-d0c2bb555b8b -Description "Demo Group Updated"
現在,如果我們再次找到群組,就會看到 Description 屬性已更新,以反映新的值:
PS C:\Windows\system32> Get-MgGroup -GroupId 958d212c-14b0-43d0-a052-d0c2bb555b8b | select displayname, description
DisplayName Description
----------- -----------
DemoGroup Demo Group Updated
刪除群組
若要從目錄中刪除群組,請使用 Remove-MgGroup Cmdlet,如下所示:
PS C:\Windows\system32> Remove-MgGroup -GroupId 958d212c-14b0-43d0-a052-d0c2bb555b8b
管理群組成員資格
新增成員
若要將新成員新增至群組,請使用 Add-MgGroupMember Cmdlet。 此命令會將成員新增至我們在上一個範例中使用的 Intune 管理員istrators 群組:
PS C:\Windows\system32> New-MgGroupMember -GroupId f76cbbb8-0581-4e01-a0d4-133d3ce9197f -DirectoryObjectId a88762b7-ce17-40e9-b417-0add1848eb68
-GroupId 參數是我們想要新增成員之群組的 ObjectID,而 -DirectoryObjectId 是我們想要新增為群組成員的使用者 ObjectID。
取得成員
若要取得群組的現有成員,請使用 Get-MgGroupMember Cmdlet,如下列範例所示:
PS C:\Windows\system32> Get-MgGroupMember -GroupId 2c52c779-8587-48c5-9d4a-c474f2a66cf4
Id DeletedDateTime
-- ---------------
71b3857d-2a23-416d-bd22-a471854ddada
fd2d57c7-22ad-42cd-961a-7340fb2eb6b4
移除成員
若要移除我們先前新增至群組的成員,請使用 Remove-MgGroupMember Cmdlet,如下所示:
PS C:\Windows\system32> Remove-MgGroupMemberByRef -DirectoryObjectId 053a6a7e-4a75-48bc-8324-d70f50ec0d91 -GroupId 2c52c779-8587-48c5-9d4a-c474f2a66cf4
驗證成員
若要確認使用者的群組成員資格,請使用 Select-MgGroupIdsUserIsMemberOf Cmdlet。 此 Cmdlet 會採用其參數來檢查群組成員資格的使用者 ObjectId,以及要檢查成員資格的群組清單。 群組清單必須以類型為 「Microsoft.Open.AzureAD.Model.GroupIdsForMembershipCheck」 的複雜變數形式提供,因此我們必須先建立具有該類型的變數:
Get-MgUserMemberOf -UserId 053a6a7e-4a75-48bc-8324-d70f50ec0d91
Id DisplayName Description GroupTypes AccessType
-- ----------- ----------- ---------- ----------
5dc16449-3420-4ad5-9634-49cd04eceba0 demogroup demogroup {Unified}
傳回的值是此使用者所屬群組的清單。 您也可以使用 Select-MgGroupIdsContactIsMemberOf、Select-MgGroupIdsGroupIdsGroupIsMemberOf 或 Select-MgGroupIdsServicePrincipalIsMemberOf,來套用此方法來檢查指定群組清單的連絡人、群組或服務主體成員資格
停用使用者建立群組
您可以防止非系統管理員使用者建立安全性群組。 Microsoft Online Directory Services (MSODS) 中的預設行為是允許非系統管理員使用者建立群組,不論自助式群組管理 (SSGM) 是否也已啟用。 SSGM 設定只會控制我的應用程式存取面板中的行為。
若要停用非系統管理員使用者的群組建立:
確認允許非系統管理員使用者建立群組:
PS C:\> Get-MgBetaDirectorySetting | select -ExpandProperty values Name Value ---- ----- NewUnifiedGroupWritebackDefault true EnableMIPLabels false CustomBlockedWordsList EnableMSStandardBlockedWords false ClassificationDescriptions DefaultClassification PrefixSuffixNamingRequirement AllowGuestsToBeGroupOwner false AllowGuestsToAccessGroups true GuestUsageGuidelinesUrl GroupCreationAllowedGroupId AllowToAddGuests true UsageGuidelinesUrl ClassificationList EnableGroupCreation true
如果傳回
EnableGroupCreation : True
,則非系統管理員使用者可以建立群組。 若要停用此功能:Install-Module Microsoft.Graph.Beta.Identity.DirectoryManagement Import-Module Microsoft.Graph.Beta.Identity.DirectoryManagement $params = @{ TemplateId = "62375ab9-6b52-47ed-826b-58e47e0e304b" Values = @( @{ Name = "EnableGroupCreation" Value = "false" } ) } Connect-MgGraph -Scopes "Directory.ReadWrite.All" New-MgBetaDirectorySetting -BodyParameter $params
管理群組的擁有者
若要將擁有者新增至群組,請使用 New-MgGroupOwner Cmdlet:
PS C:\Windows\system32> New-MgGroupOwner -GroupId 0e48dc96-3bff-4fe1-8939-4cd680163497 -DirectoryObjectId 92a0dad0-7c9e-472f-b2a3-0fe2c9a02867
-GroupId 參數是我們想要新增擁有者之群組的 ObjectID,而 -DirectoryObjectId 是我們想要新增為擁有者的使用者或服務主體的 ObjectID。
若要擷取群組的擁有者,請使用 Get-MgGroupOwner Cmdlet:
PS C:\Windows\system32> Get-MgGroupOwner -GroupId 0e48dc96-3bff-4fe1-8939-4cd680163497
Cmdlet 會傳回指定群組的擁有者清單(使用者和服務主體):
Id DeletedDateTime
-- ---------------
8ee754e0-743e-4231-ace4-c28d20cf2841
85b1df54-e5c0-4cfd-a20b-8bc1a2ca7865
4451b332-2294-4dcf-a214-6cc805016c50
如果您想要從群組中移除擁有者,請使用 Remove-MgGroupOwnerByRef Cmdlet:
PS C:\Windows\system32> Remove-MgGroupOwnerByRef -GroupId 0e48dc96-3bff-4fe1-8939-4cd680163497 -DirectoryObjectId 92a0dad0-7c9e-472f-b2a3-0fe2c9a02867
保留的別名
建立群組時,特定端點可讓使用者指定 mailNickname 或別名做為群組電子郵件地址的一部分。 具有下列高許可權電子郵件別名的群組只能由 Microsoft Entra Global 管理員istrator 建立。
- 濫用
- 管理
- administrator
- hostmaster
- 總監
- 郵政
- 根
- 安全
- 安全性
- ssl-admin
- 站長
群組回寫至內部部署 (預覽)
今天,許多群組仍會在內部部署的 Active Directory中管理。 為了回應將雲端群組同步處理回內部部署的要求,Microsoft 365 群組回寫功能適用于 Microsoft Entra ID 現在可供預覽。
Microsoft 365 群組會在雲端中建立和管理。 回寫功能可讓您將 Microsoft 365 群組當做通訊群組寫回已安裝 Exchange 的 Active Directory 樹系。 具有內部部署 Exchange 信箱的使用者接著可以從這些群組傳送和接收電子郵件。 群組回寫功能不支援 Microsoft Entra 安全性群組或通訊群組。
如需詳細資訊,請參閱 Microsoft Entra 連線 Sync 服務 的檔 。
Microsoft 365 群組回寫是 Microsoft Entra ID 的公開預覽功能,可供任何付費的 Microsoft Entra ID 授權方案使用。 如需預覽的詳細資訊,請參閱 線上服務的 通用授權條款。
下一步
您可以在 Microsoft Entra Cmdlet 找到更多 Azure Active Directory PowerShell 檔 。