使用 PowerShell 將系統管理員角色指派給 Microsoft 365 使用者帳戶
本文適用於 Microsoft 365 企業版和 Office 365 企業版。
您可以使用適用于 Microsoft 365 的 PowerShell,輕鬆地將角色指派給使用者帳戶。
針對 Graph 模組,請使用 Azure Active Directory PowerShell
首先,使用Azure AD DC 系統管理員、雲端應用程式管理員或全域系統管理員帳戶來連線到您的 Microsoft 365 租使用者。
如需詳細資訊,請參閱關於系統管理員角色。
接下來,識別您想要新增至角色的使用者帳戶登入名稱 (範例: fredsm@contoso.com) 。 這也稱為 UPN) (使用者主體名稱。
接下來,決定角色的名稱。 請參閱 Azure AD 內建角色。
注意事項
請注意本文中的附注。 Azure Active Directory (Azure AD) PowerShell 的某些角色名稱不同。 例如,Microsoft 365 系統管理中心中的SharePoint 系統管理員角色是 Azure AD PowerShell 中的SharePoint 服務管理員。
接下來,填寫登入和角色名稱,然後執行下列命令:
$userName="<sign-in name of the account>"
$roleName="<admin role name>"
$role = Get-AzureADDirectoryRole | Where {$_.displayName -eq $roleName}
if ($role -eq $null) {
$roleTemplate = Get-AzureADDirectoryRoleTemplate | Where {$_.displayName -eq $roleName}
Enable-AzureADDirectoryRole -RoleTemplateId $roleTemplate.ObjectId
$role = Get-AzureADDirectoryRole | Where {$_.displayName -eq $roleName}
}
Add-AzureADDirectoryRoleMember -ObjectId $role.ObjectId -RefObjectId (Get-AzureADUser | Where {$_.UserPrincipalName -eq $userName}).ObjectID
以下是將 SharePoint 服務管理員角色 belindan@contoso.com 指派給帳戶的已完成命令集範例:
$userName="belindan@contoso.com"
$roleName="SharePoint Service Administrator"
$role = Get-AzureADDirectoryRole | Where {$_.displayName -eq $roleName}
if ($role -eq $null) {
$roleTemplate = Get-AzureADDirectoryRoleTemplate | Where {$_.displayName -eq $roleName}
Enable-AzureADDirectoryRole -RoleTemplateId $roleTemplate.ObjectId
$role = Get-AzureADDirectoryRole | Where {$_.displayName -eq $roleName}
}
Add-AzureADDirectoryRoleMember -ObjectId $role.ObjectId -RefObjectId (Get-AzureADUser | Where {$_.UserPrincipalName -eq $userName}).ObjectID
若要顯示特定系統管理員角色的使用者名稱清單,請使用這些命令。
$roleName="<role name>"
Get-AzureADDirectoryRole | Where { $_.DisplayName -eq $roleName } | Get-AzureADDirectoryRoleMember | Ft DisplayName
使用適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組。
首先,使用全域系統管理員帳戶連線 到您的 Microsoft 365 租使用者。
針對單一角色變更
指定使用者帳戶最常見的方式是使用其顯示名稱或電子郵件名稱,也稱為其登入名稱或使用者主體名稱, (UPN) 。
顯示使用者帳戶的名稱
如果您習慣使用使用者帳戶的顯示名稱,請判斷下列資訊:
您想要設定的使用者帳戶
若要指定使用者帳戶,您必須判斷其顯示名稱。 若要取得帳戶的完整清單,請使用此命令:
Get-MsolUser -All | Sort DisplayName | Select DisplayName | More
此命令會列出使用者帳戶的顯示名稱,依顯示名稱排序,一次一個畫面。 您可以使用 Where Cmdlet,將清單篩選成較小的集合。 請參閱下列的範例。
注意事項
PowerShell Core 不支援適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組和名稱有 Msol 的 Cmdlet。 從 PowerShell 執行這些 Cmdlet。
Get-MsolUser -All | Where DisplayName -like "John*" | Sort DisplayName | Select DisplayName | More
此命令只會列出顯示名稱開頭為 「John」 的使用者帳戶。
您想要指派的角色
若要顯示您可以指派給使用者帳戶的可用系統管理員角色清單,請使用此命令:
Get-MsolRole | Sort Name | Select Name,Description
在您判斷帳戶的顯示名稱和角色的名稱之後,請使用下列命令將角色指派給帳戶:
$dispName="<The Display Name of the account>"
$roleName="<The admin role name you want to assign to the account>"
Add-MsolRoleMember -RoleMemberEmailAddress (Get-MsolUser -All | Where DisplayName -eq $dispName).UserPrincipalName -RoleName $roleName
將命令貼到記事本中。 針對 $dispName 和 $roleName 變數,請將描述文字取代為其值。 <移除 和 > 字元,但保留引號。 將修改過的行貼到 Microsoft Azure Active Directory 模組中,以Windows PowerShell視窗來執行它們。 或者,您可以使用 Windows PowerShell 整合式腳本環境 (ISE) 。
以下是已完成命令集的範例:
$dispName="Scott Wallace"
$roleName="SharePoint Service Administrator"
Add-MsolRoleMember -RoleMemberEmailAddress (Get-MsolUser -All | Where DisplayName -eq $dispName).UserPrincipalName -RoleName $roleName
使用者帳戶的登入名稱
如果您習慣使用使用者帳戶的登入名稱或 UPN,請判斷下列資訊:
使用者帳戶的 UPN
如果您不知道 UPN,請使用下列命令:
Get-MsolUser -All | Sort UserPrincipalName | Select UserPrincipalName | More
此命令會列出使用者帳戶的 UPN,依 UPN 排序,一次一個畫面。 您可以使用 Where Cmdlet 來篩選清單。 以下為範例:
Get-MsolUser -All | Where DisplayName -like "John*" | Sort UserPrincipalName | Select UserPrincipalName | More
此命令只會列出顯示名稱開頭為 「John」 的使用者帳戶。
您想要指派的角色
若要顯示您可以指派給使用者帳戶的可用角色清單,請使用此命令:
Get-MsolRole | Sort Name | Select Name,Description
在您擁有帳戶的 UPN 和角色名稱之後,請使用下列命令將角色指派給帳戶:
$upnName="<The UPN of the account>"
$roleName="<The role name you want to assign to the account>"
Add-MsolRoleMember -RoleMemberEmailAddress $upnName -RoleName $roleName
複製命令並貼到記事本中。 針對 $upnName 和 $roleName 變數。 將描述文字取代為其值。 <移除 和 > 字元,但保留引號。 將修改過的行貼到 Microsoft Azure Active Directory 模組中,Windows PowerShell視窗執行它們。 或者,您可以使用 Windows PowerShell ISE。
以下是已完成命令集的範例:
$upnName="scottw@contoso.com"
$roleName="SharePoint Service Administrator"
Add-MsolRoleMember -RoleMemberEmailAddress $upnName -RoleName $roleName
多個角色變更
如需多個角色變更,請判斷下列資訊:
您想要設定的使用者帳戶。 您可以使用上一節中的方法來收集一組顯示名稱或 UPN。
您想要指派給每個使用者帳戶的角色。 若要顯示您可以指派給使用者帳戶的可用角色清單,請使用此命令:
Get-MsolRole | Sort Name | Select Name,Description
接下來, (CSV 建立以逗號分隔的值,) 具有顯示名稱或 UPN 和角色名稱欄位的文字檔。 您可以在 Microsoft Excel 中輕鬆地執行此動作。
以下是顯示名稱的範例:
DisplayName,RoleName
"Belinda Newman","Billing Administrator"
"Scott Wallace","SharePoint Service Administrator"
接下來,填入 CSV 檔案的位置,然後在 PowerShell 命令提示字元執行產生的命令。
$fileName="<path and file name of the input CSV file that has the role changes, example: C:\admin\RoleUpdates.CSV>"
$roleChanges=Import-Csv $fileName | ForEach {Add-MsolRoleMember -RoleMemberEmailAddress (Get-MsolUser | Where DisplayName -eq $_.DisplayName).UserPrincipalName -RoleName $_.RoleName }
以下是 UPN 的範例:
UserPrincipalName,RoleName
"belindan@contoso.com","Billing Administrator"
"scottw@contoso.com","SharePoint Service Administrator"
接下來,填入 CSV 檔案的位置,然後在 PowerShell 命令提示字元執行產生的命令。
$fileName="<path and file name of the input CSV file that has the role changes, example: C:\admin\RoleUpdates.CSV>"
$roleChanges=Import-Csv $fileName | ForEach { Add-MsolRoleMember -RoleMemberEmailAddress $_.UserPrincipalName -RoleName $_.RoleName }