Cover image
Microsoft 365のユーザーにライセンスをPowerShellで一括で割り当てる

Microsoft 365のユーザーにライセンスをPowerShellで一括で割り当てる

はじめに

Microsoft 365ユーザーのライセンス付与をPowerShellで一括で行えるようにしました。

グループに対してライセンスを割り当てることができる環境であれば手動ライセンスの割当を行う必要はないのですが、それ以外の環境では重宝します。

具体的な使用例としては、新規でMicrosoft 365を導入した際や新入社員が入社してきた際に使用することが多いです。

環境

  • Microsoft 365 のテナント
  • Microsoft Graph PowerShell SDK
  • PowerShell version : 7.3.4

使用するCSVファイル

以下の表のようにライセンスを割り当てるユーザーを記載したCSVファイルを作成してください。

全員に同じライセンスを付与することを想定しています。

UserPrincipalName
user01@example.com
user02@example.com
user03@example.com

環境作成

Microsoft Graph PowerShell SDK がインストールされていない方は以下のコマンドを実行します。

# Microsoft Graph PowerShell SDK をインストールする
Install-Module Microsoft.Graph

以下のコマンド実行ポリシーを設定します。

# 実行ポリシーを設定する
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

モジュールのインポートをします。

# Microsoft Graph PowerShellモジュールのインポート
Import-Module Microsoft.Graph

ライセンス割り当て

Microsoft Graph APIに接続に接続します。

# Graphに接続
Connect-MgGraph -Scopes "Organization.Read.All","User.ReadWrite.All"

先ほど作成したCSVファイルを読み込みます。

# CSV読み込み
$CSVs = Import-Csv -Encoding UTF8 `
-Path "<ファイルへのパス>"

テナント内で使用できるすべてのライセンスを確認します。

# テナントのライセンス情報取得
Get-MgSubscribedSku | Format-Table Id, SkuId, SkuPartNumber -Wrap

以下のように出力されるので、対象のライセンスの「SkuId」をコピーします。

Id SkuId SkuPartNumber
-- ----- -------------
3fca7815-d606-4f47-a0e9-0869c232f8aa_b05e124f-c7cc-45a0-a6aa-8cf78c946968 b05e124f-c7cc-45a0-a6aa-8cf78c946968 EMSPREMIUM
3fca7815-d606-4f47-a0e9-0869c232f8aa_c7df2760-2c81-4ef7-b578-5b5392b571df c7df2760-2c81-4ef7-b578-5b5392b571df ENTERPRISEPREMIUM
3fca7815-d606-4f47-a0e9-0869c232f8aa_6fd2c87f-b296-42f0-b197-1e91e994b900 6fd2c87f-b296-42f0-b197-1e91e994b900 ENTERPRISEPACK
3fca7815-d606-4f47-a0e9-0869c232f8aa_f30db892-07e9-47e9-837c-80727f46fd3d f30db892-07e9-47e9-837c-80727f46fd3d FLOW_FREE
3fca7815-d606-4f47-a0e9-0869c232f8aa_6a0f6da5-0b87-4190-a6ae-9bb5a2b9546a 6a0f6da5-0b87-4190-a6ae-9bb5a2b9546a Win10_VDA_E3
3fca7815-d606-4f47-a0e9-0869c232f8aa_606b54a9-78d8-4298-ad8b-df6ef4481c80 606b54a9-78d8-4298-ad8b-df6ef4481c80 CCIBOTS_PRIVPREV_VIRAL
3fca7815-d606-4f47-a0e9-0869c232f8aa_184efa21-98c3-4e5d-95ab-d07053a96e67 184efa21-98c3-4e5d-95ab-d07053a96e67 INFORMATION_PROTECTION_COMPLIAN 
CE

割り当てるライセンスを変数に入れます。

# 割り当てるライセンスを定義
$addLicenses = New-Object -TypeName Microsoft.Graph.PowerShell.Models.MicrosoftGraphAssignedLicense `
-Property @{SkuId = "<割り当てるライセンスの SkuId>"}

ユーザーにライセンスを割り当てます。

# 対象のユーザーにライセンスを割り当てる
foreach($CSV in $CSVs)
{
		Set-MgUserLicense -UserId  $CSV.UserPrincipalName `
		-AddLicenses $addLicenses `
		-RemoveLicenses @()
}

確認用のコマンドを実行します。

# ライセンスが割り当たっているか確認します。
$Check_License = foreach($CSV in $CSVs)
{
    Get-MgUserLicenseDetail -UserId $CSV.UserPrincipalName `
    | Select-Object @{
        n="UserPrincipalName"
        e={$CSV.UserPrincipalName}
    },SkuPartNumber
}

変数を呼び出すと以下のように対象ユーザーのUserPrincipalNameと付与されているライセンスが出力されます。

UserPrincipalName SkuPartNumber
----------------- -------------
user01@example.com FLOW_FREE
user02@example.com FLOW_FREE
user03@example.com FLOW_FREE

証跡としてCSVファイルを作成します。

「-Encoding utf8BOM」がエラー出る場合は「-Encoding utf8」に書き換えてください。

# 確認用のCSVファイル作成
$Check_License | export-CSV -Path "<ファイルへのパス>" -Append -Encoding utf8BOM

作業がすべて終了したので、Graphから切断します。

# Graphから切断します。
Disconnect-MgGraph

所感・まとめ

対象のユーザーに対して一括でライセンスを割り当てるPowerShellを作成しました。

GUIでちまちまライセンスを割り当てる作業を効率化できたはずです。

基本的は以下の公式ドキュメントを参考にしています。不明点があれば、公式ドキュメントを見てみてください。

参考

Microsoft 365 PowerShell を使用してライセンスをユーザー アカウントに割り当てる - Microsoft 365 Enterprise
この記事では、PowerShell を使用して、ライセンスのないユーザーに Microsoft 365 ライセンスを割り当てる方法について説明します。
https://learn.microsoft.com/ja-jp/microsoft-365/enterprise/assign-licenses-to-user-accounts-with-microsoft-365-powershell?view=o365-worldwide

Set-MgUserLicense (Microsoft.Graph.Users.Actions)
Add or remove licenses for the user to enable or disable their use of Microsoft cloud offerings that the company has licenses to. For example, an organization can have a Microsoft 365 Enterprise E3 subscription with 100 licenses, and this request assigns one of those licenses to a specific user. You can also enable and disable specific plans associated with a subscription. Direct user licensing method is an alternative to group-based licensing. Permissions Permission type Permissions (from least to most privileged) Delegated (work or school account) LicenseAssignment.ReadWrite.All, User.ReadWrite.All, Directory.ReadWrite.All, Delegated (personal Microsoft account) Not supported Application LicenseAssignment.ReadWrite.All, User.ReadWrite.All, Directory.ReadWrite.All,
https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.users.actions/set-mguserlicense?view=graph-powershell-1.0