使用 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 Management Shell 及其先决条件并且已连接到 SharePoint Online。

通过下载并运行 SharePoint Online Management Shell 安装 SharePoint Online Management Shell。只需对每台计算机执行一次此操作。

若要打开 SharePoint Online Management Shell 的命令提示符,请从开始屏幕中键入 sharepoint,然后单击“SharePoint Online Management Shell”。

若要连接到 SharePoint Online,请填写 $adminUPN$orgName 变量的值(替换引号中的所有文本,包括 < and > 字符),然后在 SharePoint Online Management Shell 命令提示符中运行以下命令:

$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

此示例使用变量存储值,并在脚本中包括了注释(例如“# 这是租户名称…”),帮助你理解这些值。

例如,这组命令将 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 Management Shell”窗口中。

将用户添加到其他网站集管理员组

在此任务中,我们将使用 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

例如,从 contoso1 租赁下 Contoso Test 网站集的网站集审核员组中删除 Bobby Overby:

$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 从他当前所在的所有组中删除。方法如下:

$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 中创建报告