导出所有组的成员

$pw = ConvertTo-SecureString 'XXXXXXX' -AsPlainText -Force
$creds = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist 'zhang_san@abc.cn', $pw
Connect-MsolService -Credential $creds
Get-MsolGroup | foreach {
    $_.displayname + "," + $_.grouptype | Out-File -FilePath F:\result.csv -Append -Encoding default
    Get-MsolGroupMember -GroupObjectId $_.objectid | ForEach {
            "," + "," + $_.emailaddress  |  Out-File -FilePath F:\result.csv -Append -Encoding default
        }
}

 

2015-9-23更新, 采用了Export-Csv的方式导出文本, 使用了PSCustomObject, 哈希表等知识点.

$pw = ConvertTo-SecureString 'XXXXXXX' -AsPlainText -Force
$creds = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist 'zhang_san@abc.cn', $pw
Connect-MsolService -Credential $creds
Get-MsolGroup | ForEach-Object {
    [pscustomobject]@{displayname=$_.displayname;grouptype=$_.grouptype;mail=$null} | Export-Csv -Path F:\1.csv -Append -Encoding default -NoTypeInformation
    $members = Get-MsolGroupMember -GroupObjectId $_.objectid 
    foreach($member in $members) {
        [pscustomobject]@{displayname=$null;grouptype=$null;mail=$member.emailaddress}  |  Export-Csv -Path F:\1.csv -Append -Encoding default -NoTypeInformation
        }
}

 

posted @ 2015-08-07 13:23  dongdonggeorge  阅读(643)  评论(0编辑  收藏  举报