LoginSignup
2
2

More than 3 years have passed since last update.

Microsoft Graph PowerShell SDKを使ってみる

Last updated at Posted at 2020-10-24

Graph API って便利とは聞くけど、でもAPIってなんだか使うのが難しそう・・・というイメージがあるかもしれませんが、Microsoft Graph PowerShell SDKを使うと、PowerShellと同じ感覚で比較的容易にGraph APIが使えると思います。

事前準備

PowerShellで以下のコマンドレットを実行してモジュールをインストールします。
(環境に依存しますがモジュールのインストール完了までに10分程度かかります)

PowerShell
Install-Module Microsoft.Graph

完了したら以下のコマンドレットを実行してモジュールがインストールされていることを確認します。

PowerShell
Get-InstalledModule Microsoft.Graph*

Version    Name                                Repository           Description
-------    ----                                ----------           -----------
1.0.1      Microsoft.Graph                     PSGallery            Microsoft Graph PowerShell module
1.0.1      Microsoft.Graph.Applications        PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Authentication      PSGallery            Microsoft Graph PowerShell Authentication Module.
1.0.1      Microsoft.Graph.Bookings            PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Calendar            PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.ChangeNotifications PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.CloudCommunications PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Compliance          PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.CrossDeviceExper... PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Devices.CloudPrint  PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Devices.Corporat... PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.DirectoryObjects    PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Education           PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Files               PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Financials          PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Groups              PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Identity.Directo... PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Identity.Governance PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Identity.SignIns    PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Mail                PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Notes               PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.People              PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.PersonalContacts    PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Planner             PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Reports             PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.SchemaExtensions    PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Search              PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Security            PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Sites               PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Teams               PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Users               PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Users.Actions       PSGallery            Microsoft Graph PowerShell Cmdlets
1.0.1      Microsoft.Graph.Users.Functions     PSGallery            Microsoft Graph PowerShell Cmdlets

Microsoft Graph PowerShell SDKの使用

以下のコマンドレットを実行してGraph APIで接続してみます。
コマンドレットを実行すると、ログインサイト "https://microsoft.com/devicelogin" にアクセスしてコード(以下の hogehoge 部分)を入力するように求められるので指示に従います。

PowerShell
Connect-Graph -Scopes "User.Read.All", "Group.Read.All"
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code hogehoge to authenticate.

コードを入力するとアクセス許可を求められるので、グローバル管理者のアカウントを入力し「組織の代理として同意する」にチェックを入れて承諾します。
スクリーンショット (1).png

PowerShell SDKのコマンドレットを試してみる。例えばユーザーが所属するチームを取得するには以下のコマンドレットを実行する。

PowerShell
Get-MgUserJoinedTeam -UserId <ユーザーID>

Id                                   DisplayName                Description                                    IsArchived
--                                   -----------                -----------                                    ----------
8*******-****-****-****-***********e チーム001                   チーム001                                      False
0*******-****-****-****-***********d チーム002                   チーム001                                      False
1*******-****-****-****-***********f チーム003                   チーム001                                      False

使用できるコマンドレットの確認方法は以下。
(大量に表示されます)

PowerShell
Get-Command -Module Microsoft.Graph*

参考

Microsoft Graph PowerShell SDK をインストールする
https://docs.microsoft.com/ja-jp/graph/powershell/installation

Microsoft Graph PowerShell SDK を使い始める
https://docs.microsoft.com/ja-jp/graph/powershell/get-started

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2