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 -BodyParameter $param

更新群組

若要更新現有的群組,請使用 Update-MgGroup Cmdlet。 在這個範例中,我們要變更 “Intune Administrators” 群組的 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 Device Administrators”︰

    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 Administrators 群組︰

    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-MgGroupIdsGroupIsMemberOf 或 Select-MgGroupIdsServicePrincipalIsMemberOf 來檢查指定群組清單的連絡人、群組或服務主體成員資格

停用您使用者的群組建立

您可以避免非管理使用者建立安全性群組。 Microsoft Online 目錄服務 (MSODS) 的預設行為是可讓非管理使用者建立群組,無論是否也啟用自助式群組管理 (SSGM)。 SSGM 設定只會控制 [我的應用程式] 存取面板中的行為。

若要停用非管理使用者的群組建立:

  1. 請確認允許非管理使用者建立群組:

    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
    
  2. 如果它傳回 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建立。 

  • abuse
  • admin
  • administrator
  • hostmaster
  • majordomo
  • postmaster
  • root
  • secure
  • security
  • ssl-admin
  • webmaster

將群組回寫至內部部署 (預覽版)

目前,有許多群組仍在內部部署 Active Directory 中進行管理。 為了回應將雲端群組同步處理回內部部署的要求,Microsoft 365 群組回寫功能適用于Microsoft Entra識別碼現已可供預覽。

在雲端中建立和管理 Microsoft 365 群組。 回寫功能可讓您以通訊群組的形式,將 Microsoft 365 群組寫回至已安裝 Exchange 的 Active Directory 樹系。 具有內部部署 Exchange 信箱的使用者可以從這些群組傳送和接收電子郵件。 群組回寫功能不支援Microsoft Entra安全性群組或通訊群組。

如需詳細資訊,請參閱Microsoft Entra Connect Sync 服務的檔。

Microsoft 365 群組回寫是Microsoft Entra識別碼的公開預覽功能,可供任何付費Microsoft Entra識別碼授權方案使用。 如需預覽的詳細資訊,請參閱 線上服務的通用授權條款

下一步

您可以在Microsoft Entra Cmdlet找到更多 Azure Active Directory PowerShell 檔。