LoginSignup
5
4

More than 3 years have passed since last update.

【Office 365】PowerShellでGet-MsolUserやGet-Mailboxの実行結果をUPNでマッピング

Last updated at Posted at 2019-09-28

はじめに

Office 365にはテナント上のユーザーごとの情報(プロパティ値)を出力するPowerShellコマンドとして例えば以下のようなものがある。

  • Office 365 PowerShell
    • Get-MsolUser
  • Exchange Online PowerShell
    • Get-Mailbox
    • Get-User

上記コマンドごとに実行時に出力できるプロパティ値は異なるが、ときには複数の異なるコマンドの実行結果を一つのリストにまとめたいときがある。
その場合は各コマンドで共通して取得できるプロパティ値UserPrincipalNameをキーにしたマッピングを行えば可能である。

手順

Get-MsolUserGet-Userの結果をUPNでマッピング

例として、Get-MsolUserGet-Userでユーザーごとに以下の5つのプロパティの値をCSV出力する場合のコマンドを示す。

  • Get-MsolUser
    • DisplayName
    • UserPrincipalName
    • Licenses
  • Get-User
    • Company
    • Department

コマンド

#すべてのユーザーの情報を格納するオブジェクト$Datasを定義
$Datas = New-Object System.Collections.ArrayList

#Get-Userで取得したユーザーごとの情報を変数に格納
$ExOUsers = Get-User -ResultSize Unlimited

#Get-MsolUserで取得したユーザーごとの情報をForEachにより処理
Get-MsolUser -All | ForEach-Object {

    #UserPrincipalNameをキーにしてGet-UserとGet-MsolUserのユーザー情報を紐付け
    $MsolUPN = $_.UserPrincipalName
    $ExOUser = $ExOUsers | Where-Object {
        $_.UserPrincipalName -eq $MsolUPN
    }

    #ユーザーごとの情報を格納するオブジェクト$Dataを定義し、プロパティ値を設定して$Datasに格納
    $Data = New-Object PSObject | Select-Object DisplayName, UserPrincipalName, Company, Department, Licenses
    $Data.DisplayName = $_.DisplayName
    $Data.UserPrincipalName = $_.UserPrincipalName
    $Data.Company = ($ExOUser).Company
    $Data.Department = ($ExOUser).Department
    $Data.Licenses = ($_ | Select-Object -ExpandProperty Licenses).AccountSkuId -join ";"
    [void]$Datas.Add($Data)

}

#$Datasの内容をCSV出力
$Datas | Export-Csv export.csv -NoTypeInformation -Encoding UTF8

出力結果

export.csv
DisplayName,UserPrincipalName,Company,Department,Licenses
Liam,liam@contoso.com,contoso Inc.,marketing,hoge:FLOW_FREE;hoge:O365_BUSINESS_ESSENTIALS
Noah,noah@contoso.com,contoso Inc.,human resources,hoge:O365_BUSINESS_ESSENTIALS
Emma,emma@contoso.com,contoso Inc.,Manufacturing,
Olivia,olivia@contoso.com,contoso Inc.,Manufacturing,hoge:O365_BUSINESS_PREMIUM
Ava,ava@contoso.com,contoso Inc.,Manufacturing,hoge:O365_BUSINESS_PREMIUM;hoge:POWERAPPS_VIRAL

以上

5
4
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
5
4