Skip to content

Instantly share code, notes, and snippets.

@YasuoFromDeadScream
Created February 19, 2021 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YasuoFromDeadScream/f3ea2b2d49d3fc7f610a2eaa54cb58e5 to your computer and use it in GitHub Desktop.
Save YasuoFromDeadScream/f3ea2b2d49d3fc7f610a2eaa54cb58e5 to your computer and use it in GitHub Desktop.
function getRelation($targetRecipient, $departmentName){
$org = New-Object PSObject | Select-Object 社員名,所属部署
if($targetRecipient.RecipientType -eq "MailUniversalSecurityGroup"){
$departmentName = $targetRecipient.DisplayName
# メンバー情報の取得
Get-DistributionGroupMember $targetRecipient.Alias |
# 取得したメンバをユーザ、グループの順でソート
Select-Object Alias, DisplayName, PrimarySmtpAddress, RecipientType, @{L="SortIndex"; E={switch($_.RecipientType){
"UserMailBox"{0}
"MailUniversalSecurityGroup"{1}
}}} |
Sort-Object -Property SortIndex, DisplayName |
# 取得したメンバを使用して再帰処理
ForEach-Object {getRelation $_ $departmentName}
}else{
$org.社員名 = $targetRecipient.DisplayName -join "`r`n"
$org.所属部署 = $departmentName -join "`r`n"
[void]$orglist.Add(($org | Select-Object 社員名,所属部署 ))
}
}
Connect-ExchangeOnline -ShowBanner:$false | out-null
$global:orglist = New-Object System.Collections.ArrayList
$rooInfo = (Get-OrganizationConfig).HierarchicalAddressBookRoot
getRelation (Get-DistributionGroup $rooInfo)
$orglist | export-csv .\社員所属部署に関する情報.csv -NoTypeInformation -Encoding UTF8
Disconnect-ExchangeOnline -Confirm:$false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment