はじめに
SPFx のサイトコレクションアプリカタログを PowerShell で設定しようとしたところ、macOS + PowerShell 7 の環境でハマったのでまとめます。
問題
Windows の公式ドキュメントに載っている Microsoft.Online.SharePoint.PowerShell(SharePoint Online Management Shell)を使おうとすると、macOS + PowerShell 7 ではインストールはできてもコマンドが認識されません。
Connect-SPOService -Url "https://<tenant>-admin.sharepoint.com"
# → "The term 'Connect-SPOService' is not recognized as a name of a cmdlet..."
原因:Microsoft.Online.SharePoint.PowerShell は Windows PowerShell 5.1 ベースのモジュールです。PowerShell 7 のクロスプラットフォーム環境には非対応で、Windows であれば -UseWindowsPowerShell フラグで互換レイヤーを使う回避策がありますが、macOS にはその互換レイヤー自体が存在しません。
解決方法
macOS + PowerShell 7 ではPnP PowerShellを使います。
前提条件
テナントのアプリケーション管理者、またはグローバル管理者権限が 必須 です。
1. PnP PowerShell をインストール
Install-Module -Name PnP.PowerShell -Scope CurrentUser
2. Entra ID にアプリを登録する
PnP PowerShellの利用にはテナントにEntraアプリケーションの登録が必須です。
Connect-PnPOnline -Url "https://{tenant}.sharepoint.com" -Interactive
# → "Please specify a valid client id for an Entra ID App Registration."
といってもEntra管理センターからでなくても、Register-PnPEntraIDAppForInteractiveLoginコマンドでPowerShellから登録できます。
このコマンドで発行されたEntraアプリのClient IDを使って接続するので控えておきます。
# テナント管理者権限が必要
Register-PnPEntraIDAppForInteractiveLogin `
-ApplicationName "PnP.PowerShell" `
-Tenant {tenant}.onmicrosoft.com
# → ClientId が発行される
# App PnP.PowerShell with id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx created.
3. ClientId を指定して接続
Connect-PnPOnline `
-Url "https://<tenant>.sharepoint.com" `
-Interactive `
-ClientId {発行された ClientId}
ブラウザが開き、Microsoft アカウントでサインインすれば接続完了です。
Windows との対応表
| Windows(SPO モジュール) | macOS + PS7(PnP) | |
|---|---|---|
| モジュール | Microsoft.Online.SharePoint.PowerShell |
PnP.PowerShell |
| 接続コマンド | Connect-SPOService |
Connect-PnPOnline -Interactive -ClientId <id> |
| Entra アプリ登録 | 不要 | 必要 |
おわりに
macOS で SharePoint を PowerShell から操作する場合は、最初から PnP PowerShell 一択だと割り切ると早いです。Entra ID アプリ登録が必要な点だけ覚えておけば詰まりません。