Get-DistributionGroupandOfficeGroupInfo.ps1
<#PSScriptInfo .VERSION 1.0 .GUID 5d9f26b3-986a-4721-b4c8-24d9c47dd66c .AUTHOR aheukels .COMPANYNAME .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES Office365 .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION Get Distribution Groups of Office 365 Groups and save them to an CSV file #> Param() Param( [parameter(mandatory=$true)] [ValidateSet("Office365","ExchangeOP")] [string]$Connect, [parameter(mandatory=$true)] [ValidateSet("DistributionGroups","Office365Groups")] [string]$GroupeType, [parameter(mandatory=$false)] [string]$CSV ) function main(){ Write-Host "Connect to $Connect" if ($Connect -eq "Office365") { connect-Office365 }#end if elseif ($Connect -eq "ExchangeOP") { try { connect-ExchangeOP }#end try catch [System.Management.Automation.ParameterBindingValidationException] { Write-Host "Please check connectionUri in the script function Connect-ExchangeOP" -ForegroundColor Red }#end catch }#end else if (($connect -eq "Office365") -and ($GroupeType -eq "DistributionGroups")) { Get-Office365DistributionMembers } elseif (($connect -eq "Office365") -and ($GroupeType -eq "Office365Groups")) { Get-Office365GroupMembers } elseif (($connect -eq "ExchangeOP") -and ($GroupeType -eq "DistributionGroups")) { } elseif (($connect -eq "ExchangeOP") -and ($GroupeType -eq "Office365Groups")) { Write-Host "Office365 Groups are not supported in Exchange On-Premise" Break } if ($csv) { if (($GroupeType -eq "DistributionGroups") -and ($Connect = "Office365")) { Write-OutputDistributionGroups $csv }#end if elseif (($GroupeType -eq "Office365Groups") -and ($Connect = "Office365")) { Write-OutputO365Groups $csv }#end elseif elseif (($GroupeType -eq "DistributionGroups") -and ($Connect = "ExchangeOP")){ Write-Host "Function aan bedenken" }#end elseif elseif (($GroupeType -eq "Office365Groups") -and ($Connect = "ExchangeOP")){ Write-Host "There are no Office365Groups within Exchange On-Premise " -BackgroundColor Red }#end if } } Function connect-Office365() { if (!($365session)) { $global:o365cred=get-credential -Username $username -Message "Office 365" $global:365Session = New-PSSession -ConfigurationName Microsoft.Exchange -Credential $global:o365cred -Authentication Basic -AllowRedirection -ConnectionUri "https://outlook.office365.com/powershell-liveid" Import-PSSession $global:365session -AllowClobber -Prefix Office365 }#end if }#end function Function connect-ExchangeOP() { if (!($ExchangeOPsession)){ $global:ExchangeOPsession = New-PSSession -ConfigurationName Microsoft.Exchange -Authentication Kerberos -AllowRedirection -ConnectionUri "http://servername.lan.contoso.nl/powershell" Import-PSSession $global:ExchangeOPsession -AllowClobber -Prefix ExchangeOP }#end if }#end function function Get-Office365GroupMembers() { Try { Write-Host "Getting all the members for each O365 Group in the tenant ..." -foregroundcolor Green $O365Groups=Get-office365UnifiedGroup Write-Host "Getting all the users per Group ..." -ForegroundColor Green foreach ($O365Group in $O365Groups) { Write-Host "Members of Group: " $O365Group.DisplayName -ForegroundColor Green Get-office365UnifiedGroupLinks �Identity $O365Group.Identity �LinkType Members Write-Host } } catch [System.Exception] { Write-Host -ForegroundColor Red $_.Exception.ToString() } } function Get-Office365DistributionMembers() { Try { Write-Host "Getting all the members for each O365 Distribution Group in the tenant ..." -foregroundcolor Green $O365DistributionGroups=Get-Office365DistributionGroup Write-Host "Getting all the users per Distribution Group ..." -ForegroundColor Green foreach ($O365DistributionGroup in $O365DistributionGroups) { Write-Host "Members of Distribution Group: " $O365DistributionGroup.DisplayName -ForegroundColor Green Get-Office365DistributionGroupMember �Identity $O365DistributionGroup.Identity Write-Host } } catch [System.Exception] { Write-Host -ForegroundColor Red $_.Exception.ToString() } } function Get-OnPremiseDistributionMembers() { Try { Write-Host "Getting all the members for each O365 Distribution Group in the tenant ..." -foregroundcolor Green $OnPremisDistributionGroups=Get-ExchangeOPDistributionGroup Write-Host "Getting all the users per Distribution Group ..." -ForegroundColor Green foreach ($ExchangeOPDistributionGroup in $ExchangeOPDistributionGroups) { Write-Host "Members of Distribution Group: " $ExchangeOPDistributionGroup.DisplayName -ForegroundColor Green Get-ExchangeOPDistributionGroupMember �Identity $ExchangeOPDistributionGroup.Identity Write-Host if ($csv) { Write-OutputDistributionGroups $csv } } } catch [System.Exception] { Write-Host -ForegroundColor Red $_.Exception.ToString() } } function Write-OutputO365Groups($csv) { $output = @() $groups = get-Office365unifiedgroup | select DisplayName,PrimarySmtpAddress,AccessType Write-Host "....Please wait, writing export $csv" -ForegroundColor Green foreach ($group in $groups) { ���� $members = Get-Office365UnifiedGroupLinks $group.PrimarySmtpAddress -linktype members -resultsize unlimited ���� foreach ($member in $members) { �������� $userItem = new-object PSObject �������� $userItem | Add-Member NoteProperty -Name "GroupName" -Value $group.DisplayName �������� $userItem | Add-Member NoteProperty -Name "UserEmail" -Value $member.PrimarySmtpAddress $userItem | Add-Member NoteProperty -Name "GroupEmail" -Value $group.PrimarySmtpAddress �������� $output += $userItem $output | Export-Csv -Path $csv -NoTypeInformation ���� } } } function Write-OutputDistributionGroups($csv) { $output = @() $groups = Get-Office365DistributionGroup | select DisplayName,PrimarySmtpAddress Write-Host "....Please wait, writing export $csv" -ForegroundColor Green foreach ($group in $groups) { ���� $members = Get-Office365DistributionGroupMember -Identity $group.PrimarySmtpAddress ���� foreach ($member in $members) { �������� $userItem = new-object PSObject �������� $userItem | Add-Member NoteProperty -Name "GroupName" -Value $group.DisplayName $userItem | Add-Member NoteProperty -Name "UserEmail" -Value $member.PrimarySmtpAddress�������� $userItem | Add-Member NoteProperty -Name "GroupEmail" -Value $group.PrimarySmtpAddress �������� $output += $userItem $output | Export-Csv -Path $csv -NoTypeInformation ���� } } } Main |