使用 Office 365 PowerShell 管理 SharePoint Online 使用者和群組

 

**上次修改主題的時間:**2016-08-09

**摘要︰**使用 Office 365 PowerShell 來管理 SharePoint Online 使用者、群組及網站。

如果您是使用大型使用者帳戶或群組清單的 SharePoint Online 系統管理員,而且想要輕鬆地管理它們,您可以使用 Office 365 PowerShell。

開始之前

開始使用 Office 365 PowerShell 管理 SharePoint Online 之前,請確定已安裝 SharePoint Online 管理命令介面和其必要條件且您已連線至 SharePoint Online。

藉由下載並執行 SharePoint Online 管理命令介面 安裝 SharePoint Online 管理命令介面。一台電腦只需執行這項操作一次。

若要開啟 SharePoint Online 管理命令介面的命令提示字元,從 [開始] 畫面中輸入 sharepoint,然後按一下 SharePoint Online 管理命令介面

若要連線到 SharePoint Online,填入 $adminUPN$orgName 的變數值 (取代引號之間的所有文字,包括 < 和 > 字元),然後在 SharePoint Online 管理命令介面命令提示字元中執行下列命令︰

$adminUPN="<the full email address of an Office 365 administrator account, example: jdoe@contosotoycompany.onmicrosoft.com>"
$orgName="<name of your Office 365 organization, example: contosotoycompany>"
$userCredential = Get-Credential -UserName $adminUPN -Message "Type the password."
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $userCredential

出現 Windows PowerShell 認證要求 對話方塊提示時,輸入 Office 365 系統管理員帳戶的密碼。

現在您可以開始執行 SharePoint Online 命令。

取得網站、群組和使用者清單

開始管理使用者和群組之前,請取得網站、群組和使用者清單。接著,您可以使用此資訊逐步完成本文中的範例。

取得網站的清單

使用這個命令,來取得在您的租用戶中網站的清單︰

Get-SPOSite

取得群組的清單

使用這個命令,來取得在您的租用戶中群組的清單︰

Get-SPOSite | ForEach-Object {Get-SPOSiteGroup -Site $_.Url} |Format-Table

取得使用者的清單

使用這個命令,來取得在您的租用戶中使用者的清單︰

Get-SPOSite | ForEach-Object {Get-SPOUser -Site $_.Url}

將使用者新增至「網站集合系統管理員」群組

您可以使用 Set-SPOUser 命令,將使用者新增至網站集合上的「網站集合系統管理員」清單中。語法如下:

$tenant = "<tenant>"
# This is the Tenant Name. Value must be enclosed in double quotation marks. Example: \\"Contoso01\\"

$site = "<site>"
# This is the Site name. Value must be enclosed in double quotation marks. Example: \\"contosotest\\"

$user = "<loginname>"
# This is the users login name. Value must be enclosed in double quotation marks. Example \\"opalc\\"

Set-SPOUser -Site https://$tenant.sharepoint.com/sites/$site -LoginName $user@$tenant.onmicrosoft.com -IsSiteCollectionAdmin $true

本範例使用變數來儲存值,且在指令碼中具有附註 (例如 "# This is the Tenant Name…"),協助您了解值為何。

例如,此命令集會將 Opal Castillo (使用者名稱 opalc),新增到 contoso1 租用的 ContosoTest 網站集合系統管理員清單上︰

$tenant = "contoso1"
$site = "contosotest"
$user = "opalc"
Set-SPOUser -Site https://$tenant.sharepoint.com/sites/$site -LoginName $user@$tenant.onmicrosoft.com -IsSiteCollectionAdmin $true

您可以將上述命令實際剪貼至 [記事本],將 $tenant、$site 和 $user 的變數值,變更為環境中的實際值,再將其貼至 SharePoint Online 管理命令介面視窗內。

將使用者新增至其他「網站集合系統管理員」群組

在此工作中,我們會使用 Add-SPOUser 命令,將使用者新增至網站集合上的 SharePoint 群組。語法如下:

$tenant = "<tenant>"
# This is the Tenant Name. Value must be enclosed in double quotation marks. Example: \\"Contoso01\\"

$site = "<site>"
# This is the Site name. Value must be enclosed in double quotation marks. Example: \\"contosotest\\"

$user = "<loginname>"
# This is the users login name. Value must be enclosed in double quotation marks. Example: \\"opalc\\"

$group = "<group>"
# This is the SharePoint security Group name. Value must be enclosed in double quotation marks. Example: \\"Auditors\\"

Add-SPOUser -Group $group -LoginName $user@$tenant.onmicrosoft.com -Site https://$tenant.sharepoint.com/sites/$site

例如,讓我們將 Glen Rife (使用者名稱 glenr),新增到 contoso1 租用的 ContosoTest 網站上的「稽核員」群組內:

$tenant = "contoso1"
$site = "contosotest"
$user = "glenr"
$group = "Auditors"
Add-SPOUser -Group $group -LoginName $user@$tenant.onmicrosoft.com -Site https://$tenant.sharepoint.com/sites/$site

建立網站集合群組

您可以使用 Set-SPOSiteGroup 命令來建立新的 SharePoint 群組,並將其新增至 ContosoTest 網站集合中。語法如下:

$tenant = "<tenant>"
# This is the Tenant Name. Value must be enclosed in double quotation marks, Example: \\"Contoso01\\"

$site = "<site>"
# This is the Site name. Value must be enclosed in double quotation marks, Example: \\"contosotest\\"

$group = "<group>"
# This is the SharePoint security Group name. Value must be enclosed in double quotation marks, Example: \\"Auditors\\"

$level = "<permission level>"
# This is the level of permissions to assign to the group. Value must be enclosed in double quotation marks, Example: \\"View Only\\"

New-SPOSiteGroup -Group $group -PermissionLevels $level -Site https://$tenant.sharepoint.com/sites/$site
註記注意事項:
您必須使用引號括住任何含有空格的字串。稍後可以使用 Set-SPOSiteGroup Cmdlet 來更新群組內容 (例如權限等級)。

例如,讓我們將具有「僅限檢視」權限的「稽核員」群組,新增到 contoso1 租用的 Contoso Test 網站集合中:

$tenant = "contoso1"
$site = "Contoso Test"
$level = "View Only"
$group = "Auditors"
New-SPOSiteGroup -Group $group -PermissionLevels $level -Site https://$tenant.sharepoint.com/sites/$site

從群組中移除使用者

有時,您必須從某個網站甚至所有網站中移除使用者。可能是員工從某個部門調到另一個部門,或離開公司。您可以使用 UI,輕鬆地針對某位員工進行這項作業,但是要將整個部門從某個網站移至另一個網站時,就沒有那麼簡單了。

不過,使用 SharePoint Online 管理命令介面和 CSV 檔案,這就十分快速和簡單。在此工作中,您將使用 Windows PowerShell 從網站集合安全性群組中移除使用者。然後,使用 CSV 檔案,並移除不同網站中的許多使用者。

我們將使用 Remove-SPOUser 命令移除網站集合群組中的單一 Office 365 使用者,就如同命令語法一樣。語法如下:

$tenant = "<tenant>"
# This is the Tenant Name. Value must be enclosed in double quotation marks, Example: \\"Contoso01\\"

$site = "<site>"
# This is the Site name. Value must be enclosed in double quotation marks, Example: \\"contosotest\\"

$group = "<group>"
# This is the SharePoint security Group name. Value must be enclosed in double quotation marks, Example: \\"Auditors\\"

$user = "<loginname>"
# This is the user's login name. Value must be enclosed in double quotation marks, Example: \\"opalc\\"

Remove-SPOUser -LoginName $user@$tenant.onmicrosoft.com -Site https://$tenant.sharepoint.com/sites/$site

例如,讓我們將 Bobby Overby 從 contoso1 租用的 ContosoTest 網站上的「稽核員」群組網站集合中移除。

$tenant = "contoso1"
$site = "contosotest"
$user = "bobbyo"
$group = "Auditors"
Remove-SPOUser -LoginName $user@$tenant.onmicrosoft.com -Site https://$tenant.sharepoint.com/sites/$site -Group $group

假設我們想要從 Bobby 所在的所有群組中移除 Bobby。作法如下:

$tenant = "contoso1"
$user = "bobbyo"
Get-SPOSite | ForEach-Object {Get-SPOSiteGroup -Site $_.Url} | ForEach-Object {Remove-SPOUser -LoginName $user@$tenant.onmicrosoft.com -Site &_.Url}

注意

這只是顯示作法。除非您真地必須從每個群組中移除使用者 (例如,如果使用者離開公司),否則不應執行此命令。

自動化管理大型使用者和群組清單

若要將大量帳戶加入到 SharePoint 網站並授與權限,您可以使用 Office 365 系統管理中心、個別 PowerShell 命令或 PowerShell CSV 檔案。在這些選項中,CSV 檔案會是用以自動化此工作最快速的方法。

基本程序是建立標頭 (欄位) 對應至 Windows PowerShell 指令碼所需參數的 CSV 檔案。您可於 Microsoft Excel 2013 中輕鬆地建立這類清單,然後將其匯出為 CSV 檔案。然後,您可以使用 Windows PowerShell 指令碼逐一查看 CSV 檔案中的記錄 (列),將使用者新增至群組,再將群組新增至網站。

例如,建立 CSV 檔案來定義一組網站集合、群組和權限。接下來,會建立 CSV 檔案以將使用者填入群組中。最後,建立和執行可建立和填入群組的簡單 Windows PowerShell 指令碼。

第一個 CSV 檔案會將一個或多個群組新增至一個或多個網站集合,而其結構如下:

標頭:

Site,Group,PermissionLevels

:

https://<tenant>.sharepoint.com/sites/<site>,<site collection>,<group>,<level>

範例檔案如下:

Site,Group,PermissionLevels
https://contoso1.sharepoint.com/sites/contosotest,Contoso Project Leads,Full Control
https://contoso1.sharepoint.com/sites/contosotest,Contoso Auditors,View Only
https://contoso1.sharepoint.com/sites/contosotest,Contoso Designers,Design
https://contoso1.sharepoint.com/sites/TeamSite01,XT1000 Team Leads,Full Control
https://contoso1.sharepoint.com/sites/TeamSite01,XT1000 Advisors,Edit
https://contoso1.sharepoint.com/sites/Blog01,Contoso Blog Designers,Design
https://contoso1.sharepoint.com/sites/Blog01,Contoso Blog Editors,Edit
https://contoso1.sharepoint.com/sites/Project01,Project Alpha Approvers,Full Control

第二個 CSV 檔案會將一個或多個使用者新增至一個或多個群組,而其結構如下:

標頭:

Group,LoginName,Site

:

<group>,<login>,https://<tenant>.sharepoint.com/sites/<site>

範例檔案如下:

Group,LoginName,Site
Contoso Project Leads,bobbyo@contoso1.onmicrosoft.com,https://contoso1.sharepoint.com/sites/contosotest
Contoso Auditors,allieb@contoso1.onmicrosoft.com,https://contoso1.sharepoint.com/sites/contosotest
Contoso Designers,bonniek@contoso1.onmicrosoft.com,https://contoso1.sharepoint.com/sites/contosotest
XT1000 Team Leads,dorenap@contoso1.onmicrosoft.com,https://contoso1.sharepoint.com/sites/TeamSite01
XT1000 Advisors,garthf@contoso1.onmicrosoft.com,https://contoso1.sharepoint.com/sites/TeamSite01
Contoso Blog Designers,janets@contoso1.onmicrosoft.com,https://contoso1.sharepoint.com/sites/Blog01
Contoso Blog Editors,opalc@contoso1.onmicrosoft.com,https://contoso1.sharepoint.com/sites/Blog01
Project Alpha Approvers,robinc@contoso1.onmicrosoft.com,https://contoso1.sharepoint.com/sites/Project01

下一個步,您必須將這兩個 CSV 檔案儲存至磁碟機。以下的命令會使用這兩個 CSV 檔案,來新增權限和群組成員︰

Import-Csv C:\O365Admin\GroupsAndPermissions.csv | ForEach-Object {New-SPOSiteGroup -Group $_.Group -PermissionLevels $_.PermissionLevels -Site $_.Site}
Import-Csv C:\O365Admin\Users.csv | ForEach-Object {Add-SPOUser -Group $_.Group -LoginName $_.LoginName -Site $_.Site}

指令碼會匯入 CSV 檔案內容,並使用粗體欄位中的值,填入 New-SPOSiteGroupAdd-SPOUser 命令的參數中。在範例中,我們會將這項資訊儲存至磁碟機 C,但您可將其儲存至您想要的位置。

現在,請使用相同的 CSV 檔案,來移除不同網站中數個群組的一群人。命令如下:

Import-Csv C:\O365Admin\Users.csv | ForEach-Object {Remove-SPOUser -LoginName $_.LoginName -Site $_.Site -Group $_.Group}

產生使用者報告

您可能會想要取得許多網站的簡單報告,並顯示那些網站的使用者、其權限等級及其他內容。語法如下:

$tenant = "<tenant>"
# This is the Tenant Name. Value must be enclosed in double quotes, Example: \\"Contoso01\\"

$site = "<site>"
# This is the Site name. Value must be enclosed in double quotes, Example: \\"contosotest\\"

Get-SPOUser -Site https://$tenant.sharepoint.com/sites/$site | select * | Format-table -Wrap -AutoSize | Out-File c\UsersReport.txt -Force -Width 360 -Append

這樣會抓取這三個網站的資料,並將它們寫入您本機磁碟機上的文字檔。請注意,參數 –Append 會將新的內容新增至現有檔案。

例如,讓我們在 Contoso1 租用戶的 ContosoTest、TeamSite01 和 Project01 網站上執行報表:

$tenant = "contoso1"
$site = "contosotest"

Get-SPOUser -Site https://$tenant.sharepoint.com/sites/$site | Format-Table -Wrap -AutoSize | Out-File c:\UsersReport.txt -Force -Width 360 -Append

$site = "TeamSite01"

Get-SPOUser -Site https://$tenant.sharepoint.com/sites/$site |Format-Table -Wrap -AutoSize | Out-File c:\UsersReport.txt -Force -Width 360 -Append

$site = "Project01"

Get-SPOUser -Site https://$tenant.sharepoint.com/sites/$site | Format-Table -Wrap -AutoSize | Out-File c:\UsersReport.txt -Force -Width 360 -Append

請注意,我們只需要變更 $site 變數。在這三次執行命令期間,$tenant 變數都會保留其值。

不過,如果您想要對每個網站執行這項作業,該怎麼辦?使用此命令,便可執行這項作業,無需鍵入所有這些網站:

Get-SPOSite | ForEach-Object {Get-SPOUser -Site $_.Url} | Format-Table -Wrap -AutoSize | Out-File c:\UsersReport.txt -Force -Width 360 -Append

此報告相當簡單,而且您可以新增更多的程式碼,以建立更特定的報告或是含有更詳細資訊的報告。但這應該足以讓您知道,如何使用 SharePoint Online 管理介面,來管理 SharePoint Online 環境中的使用者。

請參閱

使用 Office 365 PowerShell 管理 Office 365
開始使用 Office 365 PowerShell
使用 Office 365 PowerShell 管理使用者帳戶
使用 Windows PowerShell 在 Office 365 中建立報告