Set-ExecutionPolicy unrestricted
$cred = Get-Credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $session
get-distributiongroup -resultsize unlimited |ft name, managedby
$Groups = Get-DistributionGroup
$Groups | ForEach-Object {$group = $_.Name
$members = ''
Get-DistributionGroupMember $group | ForEach-Object {
If($members) {
$members=$members + ";" + $_.Name
} Else {
$members=$_.Name
}
}
导出组一
$Groups | ForEach-Object {
$group = $_.Name
$members = ''
Get-DistributionGroupMember $group | ForEach-Object {
If($members) {
$members=$members + ";" + $_.Name
} Else {
$members=$_.Name
}
}
New-Object -TypeName PSObject -Property @{
GroupName = $group
Members = $members
}
} | Export-CSV "D:\Distribution-Group-Members.csv" -NoTypeInformation -Encoding UTF8
导出组二
$file_name = "D:\Groups.csv"
add-content -value ("Distribution List"+","+"Display Name"+","+"Group Type"+","+"Time"+","+"Message Approval"+","+"Member") -path $file_name
$DL=get-DistributionGroup
foreach ($_ in $DL)
{
$name = $_.DisplayName
$ID = $_.PrimarySmtpAddress
$Type = $_.RecipientType
$modified = $_.WhenChanged
$DLmember=get-DistributionGroupMember -identity $_.identity
foreach($_ in $DLmember)
{
$members = $_.PrimarySmtpAddress
add-content -value ($ID +","+$name +","+$Type +","+$modified +","+$members) -path $file_name}
}
}