Skip to content

Instantly share code, notes, and snippets.

@kazusato
Last active August 24, 2022 00:37
Show Gist options
  • Save kazusato/00ac9218c4d9a19997fc65416a67805c to your computer and use it in GitHub Desktop.
Save kazusato/00ac9218c4d9a19997fc65416a67805c to your computer and use it in GitHub Desktop.
PowerShell によるAzure AD操作

モジュールの種類

Azure AD用モジュールの種類については、以下のページが参考になる(2017年と若干古いが)。

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

このドキュメントに記載されている通り、Azure AD関連のPowerShellモジュールは以下の3種類がある。

  • MSOnline
  • AzureAD
  • AzureADPreview

現状、特に理由がなければAzureAD(プレビュー機能も使うならAzureADPreview)にすべき模様。

モジュールの導入

前提チェック

上記ドキュメントに記載があるが、確認方法の欄に、いきなり「インストールします」と出てきたりしてわかりにくいが、 .NET FrameworkとPowerShellのバージョンチェックをすればよい。

導入

管理者でPowerShellを開き、

> Install-Module -Name AzureAD
または
> Install-Module -Name AzureADPreview

を実行すればよい。

実行

接続

以下のコマンドを実行すると、ログイン画面がポップアップするので、当該AADテナントを操作できるアカウントでログインする。

> Connect-AzureAD -TenantId <接続先のAADテナントID>

※当該AADテナントのアカウントでログインする場合は不要かもしれないが、接続先のAADテナントIDを明示的に指定しないと、 ログインしても当該テナントのアカウント操作が権限なし(下記エラー)で実行できなかった。

PS C:\WINDOWS\system32> Get-AzureADUser -ObjectId dummy@dummy.onmicrosoft.com                               
Get-AzureADUser : Error occurred while executing GetUser
Code: Authentication_Unauthorized
Message: User was not found.
RequestId: 00000000-0000-0000-0000-000000000000
DateTimeStamp: Sun, 11 Oct 2020 10:11:12 GMT
HttpStatusCode: Forbidden
HttpStatusDescription: Forbidden
HttpResponseStatus: Completed
発生場所 行:1 文字:1
+ Get-AzureADUser -ObjectId dummy@dummy.onmicrosoft.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-AzureADUser], ApiException
    + FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.GetUser

アカウント情報の参照

> Get-AzureADUser -ObjectId dummy@dummy.onmicrosoft.com

パスワードポリシーの確認

> Get-AzureADUser -ObjectId proggen0001@kazusatodevadmin.onmicrosoft.com  | Select-Object UserprincipalName,@{
    N="PasswordNeverExpires";E={$_.PasswordPolicies}
} 

※パスワードポリシーは、パスワードを無期限にするために使用する。以下のページ参照。

https://docs.microsoft.com/ja-jp/microsoft-365/admin/add-users/set-password-to-never-expire?view=o365-worldwide

なお、パスワードポリシー項目には「DisablePasswordExpiration」「DisableStrongPassword」の2種類の値(どちらか一方または両者を カンマ区切りで連結した値)が指定可能。 詳しくは以下のページの「要求本文」の表でpasswordPoliciesの項を参照のこと。

https://docs.microsoft.com/ja-jp/graph/api/user-update?view=graph-rest-1.0&tabs=java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment