Office 365 PowerShell を使用して SharePoint Online のユーザーとグループを管理する

 

**概要:**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 をダウンロードして実行してください。これはコンピューターごとに 1 回だけ実行する必要があります。

SharePoint Online Management Shell コマンド プロンプトを開くには、スタート画面で「sharepointと入力してから、[SharePoint Online Management Shell] をクリックします。

SharePoint Online に接続するには、$adminUPN 変数と $orgName 変数の値を入力し (< や > の文字など、引用符の間のテキストをすべて置き換える)、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

次の例は、変数を使用して値を格納します。スクリプトには、値の意味を理解するために役立つメモ (たとえば、「# 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 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 コマンドレットを使用して更新できます。

たとえば、表示のみのアクセスが許可される監査グループを 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

グループからユーザーを削除する

場合によっては、あるサイトまたはすべてのサイトからユーザーを削除する必要があります。たとえば、従業員がある部署から別の部署に異動した場合や、退職した場合などが該当します。対象の従業員が 1 人のみの場合は UI で簡単に削除できますが、部署全体をあるサイトから別のサイトに移動する場合は容易ではありません。

けれども SharePoint Online 管理シェルと CSV ファイルを使用すれば、短時間かつ簡単に終わらせることができます。このタスクでは、まず、Windows PowerShell を使用して、サイト コレクションのセキュリティ グループから 1 人のユーザーを削除します。次に、CSV ファイルを使用して、複数の異なるサイトから多数のユーザーを削除します。

まず、コマンドの構文を確認できるように、Remove-SPOUser コマンドを使用して単一の Office 365 ユーザーを 1 つのサイト コレクションのグループから削除します。以下に、構文の一例を示します。

$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 コマンド、または CSV ファイルの PowerShell を使用します。これらの選択肢の中では、CSV ファイルがこのタスクを自動化する最も簡単な方法になります。

基本的なプロセスは、CSV を作成し、ヘッダー (列) を Windows PowerShell スクリプトに必要なパラメーターに対応させることです。Microsoft Excel 2013 では、このようなリストを簡単に作成して、CSV ファイルとしてエクスポートできます。次に、Windows PowerShell スクリプトを使用して CSV ファイル内のレコード (行) を繰り返し処理し、ユーザーをグループに、グループをサイトに追加していきます。

例として、サイト コレクション、グループ、アクセス許可を定義する CSV ファイルを作成します。次に、グループにユーザーを取り込むための CSV ファイルを作成します。最後に、グループを作成してユーザーを取り込む単純な Windows PowerShell スクリプトを作成して実行します。

最初の CSV ファイルは、1 つ以上のグループを 1 つ以上のサイト コレクションに追加します。このファイルの構造は以下のとおりです。

ヘッダー:

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

2 番目の CSV ファイルは、1 つ以上のユーザーを 1 つ以上のグループに追加します。このファイルの構造は以下のとおりです。

ヘッダー:

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

次の手順では、2 つの 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-SPOSiteGroup および Add-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

上記のコードは、3 つのサイトのデータを取得して、ローカル ドライブ上のテキスト ファイルに書き込みます。パラメーター –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 変数は、このコマンドの 3 つの実行すべてを通じてその値を保持します。

一方、この操作をすべてのサイトに対して行うとしたら、どうなるでしょうか。以下のコードを使用すれば、すべての Web サイトを入力することなく、このコマンドを使用できます。

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 でレポートを作成する