見出し画像

AADユーザ作成と365ライセンス付与コマンドをMSOnlineからGraph PowerShellに移行してみたら意外とつまずいたし、つまずいてる

@juntakata氏のこのツイートを見て「あ、これ使ってるやつや」と気付きました。
Microsoft 365 のユーザ作成とライセンス付与のコマンドです。
うちではこのコマンドだけが対象となっていて、移行を試してみました。

ライセンス割り当てに関する MSOL / Azure AD PowerShell コマンド(Set-MsolUserLicense や Set-AzureADUserLicense など)は、 2023/3/31 に利用できなくなる見込みです。

ライセンス割り当てに関するコマンド以外(例: Connect-MsolService や Get-AzureADUser など)は、2022/12 以降に廃止となる予定です。

https://jpazureid.github.io/blog/azure-active-directory/azuread-module-retirement1/

▼ 自分に関係あるのか?

そもそも自分たちは関係するコマンドをツールに組み込んでいたんだっけ?という調査は、棚卸する以外の方法が紹介されていました。

例えば、サインインログを使用する方法。
Connect-MsolService で AAD に対話的に接続している場合(PowerShell にコマンドをコピペしている場合とか)、Azureポータル の サインインログ を“Azure Active Directory PowerShell” (AppId:
1b730954-1685-4b74-9bfd-dac224a7b894)でフィルターすると使用日時が特定できるので「この日の作業は何だったっけ?」から探せるとのこと(探せる?)。

参考:Azure AD Graph / MSOnline PowerShell モジュール利用状況の調べ方 | Japan Azure Identity Support Blog (jpazureid.github.io)

▼ MSOnline Powershell でユーザを作成してライセンスを割り当てる

New-MsolUser で -LicenseAssignment を使ってユーザ作成とライセンス付与していました。こんなコマンドでした。

New-MsolUser -DisplayName "織田 信忠" -LastName "織田" -FirstName "信忠" -UserPrincipalName nobut_oda@conto.onmicrosoft.com -UsageLocation JP -Country JP -PreferredLanguage ja-JP -LicenseAssignment conto:O365_BUSINESS_PREMIUM -Password H0gehoge

▼ Microsoft Graph PowerShell のインストール

今後は Microsoft Graph PowerShell を使えばいいそうです。
ついにGraphか~。
ここにヒントがあったので、それにならってモジュールのインストールからやっていきます。

参考:Microsoft Graph PowerShell SDK を使用したライセンス管理操作の紹介 | Japan Azure Identity Support Blog (jpazureid.github.io)

モジュールをインストール

Install-Module Microsoft.Graph

Connect-MgGraph で Microsoft Graph に接続する
Azure AD への初回認証時に権限を追加する

Connect-MgGraph -Scopes "Organization.Read.All","User.ReadWrite.All"

初めて Connect-MgGraph する際はアプリケーション認証のため
-Scopes 以下が必要で、これがないと「Insufficient privileges to complete the operation」エラーでユーザを作成する権限がたりません。
一度アプリケーションに権限を登録しておけば、
次回以降は Connect-MgGraph だけでライセンス付与できます。

Import-Module -Name Microsoft.Graph.Users

作業終了時

Disconnect-MgGraph

▼ Microsoft Graph PowerShell でユーザを作成してライセンスを割り当てる

New-MgUser を使ってユーザを作成します。

参考:ユーザーを作成する - Microsoft Graph v1.0 | Microsoft Learn
参考:New-MgUser (Microsoft.Graph.Users) | Microsoft Docs

New-MsolUser と New-MgUser では、
姓名やサインイン時パスワード変更などの指定方法が変わっていました。

例:New-MsolUser → New-MgUser

  • -FirstName "信忠" → -GivenName "信忠"

  • -LastName "織田" → -SurName "織田"

  • -Password H0gehoge(ユーザの初回サインイン時にパスワードを変更させる -ForceChangePassword がデフォルトで True なので省略した) → -PasswordProfile @{ForceChangePasswordNextSignIn = $true; Password = "H0gehoge"}(ユーザの初回サインイン時にパスワードを変更させる -ForceChangePasswordNextSignIn はデフォルトで False なので明示する)

  • → -MailNickname nobut-oda が必要(メールの@より左に使う文字列)

  • → -AccountEnabled が必要

  • -LicenseAssignment conto:O365_BUSINESS_PREMIUM →  -AddLicenses @{SkuId = "skuid1234"}

ライセンスを指定するためのSkuIdはここで調べられる:ライセンスを付与するための製品名とサービス プラン識別子 - Azure AD - Microsoft Entra | Microsoft Docs

・試したこと1 New-MgUser -AssignedLicenses

New-MgUser でユーザを作成する際に
-AssignedLicenses というパラメータがあったので、もしかしてこれでライセンス追加できるのでは?と期待したが read-only らしく失敗。

New-MgUser -DisplayName "織田 信忠" -SurName "織田" -GivenName "信忠" -UserPrincipalName nobut_oda@conto.onmicrosoft.com -UsageLocation JP -Country JP -PreferredLanguage ja-JP -PasswordProfile @{ForceChangePasswordNextSignIn = $true; Password = "Hogehoge"} -MailNickname nobut_oda -AccountEnabled -AssignedLicenses @{SkuId = "c42b9cae-ea4f-4ab7-9717-81576235ccac"}
New-MgUser : Property 'assignedLicenses' is read-only and cannot be set.

・試したこと2 New-MgUser | Set-MgUserLicense

ライセンスを付与するために Set-MgUserLicense を使うことにしました。
作成したユーザをそのままパイプで渡そうとすると失敗。

参考:Set-MgUserLicense (Microsoft.Graph.Users.Actions) | Microsoft Docs

New-MgUser -DisplayName "織田 信忠" -SurName "織田" -GivenName "信忠" -UserPrincipalName nobut_oda@conto.onmicrosoft.com -UsageLocation JP -Country JP -PreferredLanguage ja-JP -PasswordProfile @{ForceChangePasswordNextSignIn = $true; Password = "H0gehoge"} -MailNickname nobut_oda -AccountEnabled | Set-MgUserLicense -AddLicenses @{SkuId = "c42b9cae-ea4f-4ab7-9717-81576235ccac"} -RemoveLicenses @()
Set-MgUserLicense : パイプラインが停止されています。
New-MgUser : InputObject has null value for InputObject.UserId
Set-MgUserLicense : InputObject has null value for InputObject.UserId

・試したこと3 New-MgUser | Set-MgUserLicense -UserId $_.Id

New-MgUser からの出力は Id であるのに対して、
Set-MgUserLicense が求める入力は UserId であるため、
パイプでわたすには Id を UserId として指定すれば良いらしい。
しかし、Id が空だと言われ失敗。
コマンドが読み込まれた時点では Id が確定していないから?
参考:Results from Get-MgUser can't be piped to other MgUser functions · Issue #1182 · microsoftgraph/msgraph-sdk-powershell · GitHub

New-MgUser -DisplayName "織田 信忠" -SurName "織田" -GivenName "信忠" -UserPrincipalName nobut_oda@conto.onmicrosoft.com -UsageLocation JP -Country JP -PreferredLanguage ja-JP -PasswordProfile @{ForceChangePasswordNextSignIn = $true; Password = "H0gehoge"} -MailNickname nobut_oda -AccountEnabled | Set-MgUserLicense -UserId $_.Id -AddLicenses @{SkuId = "c42b9cae-ea4f-4ab7-9717-81576235ccac"} -RemoveLicenses @()
Set-MgUserLicense : 引数が空の文字列であるため、パラメーター 'UserId' にバインドできません。

・試したこと4 New-MgUser; Set-MgUserLicense 結局こうした

結局、2行にわけてコマンドを作成してお茶を濁しました。

New-MgUser -DisplayName "織田 信忠" -SurName "織田" -GivenName "信忠" -UserPrincipalName nobut_oda@conto.onmicrosoft.com -UsageLocation JP -Country JP -PreferredLanguage ja-JP -PasswordProfile @{ForceChangePasswordNextSignIn = $true; Password = "H0gehoge"} -MailNickname nobut_oda -AccountEnabled
Set-MgUserLicense -UserId nobut_oda@conto.onmicrosoft.com -AddLicenses @{SkuId = "c42b9cae-ea4f-4ab7-9717-81576235ccac"} -RemoveLicenses @()

▼ 感想

時間をかけずにアカウントを発行できれば手段など何でもいい(だから検証に時間がかかるならGUIポチポチやれ)、という気持ちもわかるのですが、誰がやってもミスなく再現性よく処理できるように、アカウント申請フローが承認されたら自動でアカウント発行と完了連絡までできる状態に近づけたいのです。

(それにしても、パイプでつなげて書けるようになりたいなあ。なんか悔しいなあ。)

▼ つづきの投稿


この記事が気に入ったらサポートをしてみませんか?