LoginSignup
2
8

More than 3 years have passed since last update.

【Office 365】階層型アドレス帳の階層構造をリストに出力するPowerShellスクリプトを作成した

Last updated at Posted at 2019-11-02

はじめに

Office 365(Exchange Online)の階層型アドレス帳の階層構造をリストに出力するPowerShellスクリプトを作成したのでメモ。
加工しやすさ重視のリストを出力するスクリプトとパッと見の分かりやすさ重視のリストを出力するスクリプトの2パターンで作成してみた。

手順作成は以下のような階層型アドレス帳に対して行った。(作成環境のライセンスが足りなかったのでユーザーの代わりに連絡先を使っている)
image.png

スクリプト

加工しやすさ重視のスクリプト

スクリプトは以下の通り。

GetGalStructure.ps1
#getGroupTree関数定義
function getGroupTree($targetRecipient, $upperHierarchy){

    #$Datasに現在の$targetRecipientの情報を追記
    $Data = New-Object PSObject | Select-Object upperHierarchy, DisplayName, PrimarySmtpAddress, Title, RecipientType
    $Data.upperHierarchy = $upperHierarchy -join " - "
    $Data.DisplayName = $targetRecipient.DisplayName
    $Data.PrimarySmtpAddress = $targetRecipient.PrimarySmtpAddress
    $Data.Title = $targetRecipient.Title
    $Data.RecipientType = $targetRecipient.RecipientType
    [void]$Datas.Add($Data)

    #$targetRecipientがグループの場合
    if($targetRecipient.RecipientType -eq "MailUniversalSecurityGroup"){

        #$upperHierarchyに$targetRecipientを追加       
        $upperHierarchy += @($targetRecipient.DisplayName)

        #$targetRecipientのメンバー取得
        Get-DistributionGroupMember $targetRecipient.Alias |
            #取得したメンバーをユーザー、連絡先、グループの順で並び替え
            Select-Object Alias, DisplayName, PrimarySmtpAddress, Title, RecipientType, @{L="SortIndex"; E={switch($_.RecipientType){
                "UserMailBox"{0}
                "MailContact"{1}
                "MailUniversalSecurityGroup"{2}
            }}} |
            Sort-Object -Property SortIndex, DisplayName |
            #取得したメンバーに対してgetGroupTree関数を再帰実行
            ForEach-Object {getGroupTree $_ $upperHierarchy}

    }
}

#取得したGAL構造を格納するオブジェクト$Datasを定義
$global:Datas = New-Object System.Collections.ArrayList

#GALルート取得、チェック
$treeRootName = (Get-OrganizationConfig).HierarchicalAddressBookRoot
if([string]::IsNullOrEmpty($treeRootName)){"HierarchicalAddressBookRoot未指定。スクリプトの実行を中断します。";exit}

#GALルートに対してgetGroupTree関数を実行
getGroupTree (Get-DistributionGroup $treeRootName)

Exchange Online PowerShellにリモート接続した上で、GetGalStructure.ps1を実行。

PS > .\GetGalStructure.ps1

$Datasに階層構造のリストが格納されるのでOut-GridViewExport-Csvでリストを出力すればよい。

Out-GridViewで出力する場合。

PS > $Datas | Out-GridView

Excelなどで加工して二次利用はしやすいが、パッと見で階層構造が分かりづらいかも。

image.png

パッと見の分かりやすさ重視のスクリプト

スクリプトは以下の通り。

GetGalStructure2.ps1
#getGroupTree関数定義
function getGroupTree($targetRecipient, $upperHierarchy){

    #現在の$targetRecipientを格納するオブジェクト$Dataを定義
    $Data = New-Object PSObject | Select-Object upperHierarchy, DisplayName, PrimarySmtpAddress, Title, RecipientType
    $Data.PrimarySmtpAddress = $targetRecipient.PrimarySmtpAddress
    $Data.RecipientType = $targetRecipient.RecipientType

    #$targetRecipientがグループの場合
    if($targetRecipient.RecipientType -eq "MailUniversalSecurityGroup"){

        #$upperHierarchyに$targetRecipientを追加
        $upperHierarchy += @($targetRecipient.DisplayName)

        #$Datasに$targetRecipientの情報を追記
        $Data.upperHierarchy = $upperHierarchy -join " - "
        [void]$Datas.Add($Data)

        #$targetRecipientのメンバー取得
        Get-DistributionGroupMember $targetRecipient.Alias |
            #取得したメンバーをユーザー、連絡先、グループの順で並び替え
            Select-Object Alias, DisplayName, PrimarySmtpAddress, Title, RecipientType, @{L="SortIndex"; E={switch($_.RecipientType){
                "UserMailBox"{0}
                "MailContact"{1}
                "MailUniversalSecurityGroup"{2}
            }}} |
            Sort-Object -Property SortIndex, DisplayName |
            #取得したメンバーに対して関数を再帰実行
            ForEach-Object {getGroupTree $_ $upperHierarchy}

    #$targetRecipientがその他受信者の場合
    }else{

        #$Datasに$targetRecipientの情報を追記
        $Data.DisplayName = $targetRecipient.DisplayName
        $Data.Title = $targetRecipient.Title
        [void]$Datas.Add($Data)

    }

}

#取得したGAL構造を格納するオブジェクト$Datasを定義
$global:Datas = New-Object System.Collections.ArrayList

#GALルート取得、チェック
$treeRootName = (Get-OrganizationConfig).HierarchicalAddressBookRoot
if([string]::IsNullOrEmpty($treeRootName)){"HierarchicalAddressBookRoot未指定。スクリプトの実行を中断します。";exit}

#GALルートに対してgetGroupTree関数を実行
getGroupTree (Get-DistributionGroup $treeRootName)

Exchange Online PowerShellにリモート接続した上で、GetGalStructure2.ps1を実行。

PS > .\GetGalStructure2.ps1

こちらも同様に$Datasに階層構造のリストが格納されるのでOut-GridViewExport-Csvでリストを出力すればよい。

Out-GridViewで出力する場合。

PS > $Datas | Out-GridView

先程のリストに比べてパッと見で階層構造が分かりやすいが、そのために出力を省いている列があるので、Excelなどで加工はしにくいかも。

image.png

おわりに

要望があればSeniorityIndexも含むリストを出力するスクリプトも作って紹介するかも。(Get-DistributionGroupMemberではSeniorityIndexプロパティが取得できないため、GALに含まれるオブジェクトごとに関数内でさらにGetコマンドを実行する必要があり、実行時の負担が結構大きい。)

以上

2
8
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
8