导出所有组的成员
$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 } }