From c9f81c5191d88b58d7ac166176176fcd2ee1a53a Mon Sep 17 00:00:00 2001 From: AdminDroid <49208841+admindroid-community@users.noreply.github.com> Date: Tue, 13 Dec 2022 14:27:10 +0530 Subject: [PATCH] Export Distribution Group Members Report Export Distribution Group Members Report --- .../GetDistributionGroupMembers.ps1 | 97 +++++++------------ 1 file changed, 37 insertions(+), 60 deletions(-) diff --git a/Office 365 Distribution Group Members Report/GetDistributionGroupMembers.ps1 b/Office 365 Distribution Group Members Report/GetDistributionGroupMembers.ps1 index 9540261..74ec855 100644 --- a/Office 365 Distribution Group Members Report/GetDistributionGroupMembers.ps1 +++ b/Office 365 Distribution Group Members Report/GetDistributionGroupMembers.ps1 @@ -1,10 +1,17 @@ -Param +<# +============================================================================================= +Name: Get distribution group members report +Version: 3.0 +Website: o365reports.com +For detailed script execution: https://o365reports.com/2019/05/23/export-office-365-distribution-group-members-csv/ +============================================================================================ +#> +Param ( [Parameter(Mandatory = $false)] [string]$GroupNamesFile, [switch]$IsEmpty, [int]$MinGroupMembersCount, - [switch]$MFA, [Nullable[boolean]]$ExternalSendersBlocked = $null, [string]$UserName, [string]$Password @@ -151,66 +158,35 @@ Function main() #Clean up session Get-PSSession | Remove-PSSession - #Authentication using MFA - if($MFA.IsPresent) + #Check for EXO v2 module inatallation + $Module = Get-Module ExchangeOnlineManagement -ListAvailable + if($Module.count -eq 0) + { + Write-Host Exchange Online PowerShell V2 module is not available -ForegroundColor yellow + $Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No + if($Confirm -match "[yY]") + { + Write-host "Installing Exchange Online PowerShell module" + Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force + Import-Module ExchangeOnlineManagement + } + else + { + Write-Host EXO V2 module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet. + Exit + } + } + Write-Host Connecting to Exchange Online... + #Storing credential in script for scheduling purpose/ Passing credential as parameter - Authentication using non-MFA account + if(($UserName -ne "") -and ($Password -ne "")) { - $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) - If ($MFAExchangeModule -eq $null) - { - Write-Host `nPlease install Exchange Online MFA Module. -ForegroundColor yellow - - Write-Host You can install module using below blog : `nLink `nOR you can install module directly by entering "Y"`n - $Confirm= Read-Host Are you sure you want to install module directly? [Y] Yes [N] No - if($Confirm -match "[yY]") - { - Write-Host Yes - Start-Process "iexplore.exe" "https://cmdletpswmodule.blob.core.windows.net/exopsmodule/Microsoft.Online.CSE.PSModule.Client.application" - } - else - { - Start-Process 'https://http://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/' - Exit - } - $Confirmation= Read-Host Have you installed Exchange Online MFA Module? [Y] Yes [N] No - - if($Confirmation -match "[yY]") - { - $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) - If ($MFAExchangeModule -eq $null) - { - Write-Host Exchange Online MFA module is not available -ForegroundColor red - Exit - } - } - else - { - Write-Host Exchange Online PowerShell Module is required - Start-Process 'https://http://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/' - Exit - } - } - - #Importing Exchange MFA Module - . "$MFAExchangeModule" - Write-Host Enter credential in prompt to connect to Exchange Online - Connect-EXOPSSession -WarningAction SilentlyContinue - Write-Host `nReport generation in progress... + $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force + $Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword + Connect-ExchangeOnline -Credential $Credential } - #Authentication using non-MFA else { - #Storing credential in script for scheduling purpose/ Passing credential as parameter - if(($UserName -ne "") -and ($Password -ne "")) - { - $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force - $Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword - } - else - { - $Credential=Get-Credential -Credential $null - } - $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection - Import-PSSession $Session -CommandName Get-DistributionGroup,Get-DistributionGroupMember -FormatTypeName * -AllowClobber | Out-Null + Connect-ExchangeOnline } #Set output file @@ -250,6 +226,7 @@ Function main() { Write-Host Detailed report available in: $ExportCSV Write-host Summary report available in: $ExportSummaryCSV + Write-Host `nCheck out """AdminDroid Office 365 Reporting tool""" to get access to 1500+ Office 365 reports.`n -ForegroundColor Green $Prompt = New-Object -ComObject wscript.shell $UserInput = $Prompt.popup("Do you want to open output file?",` 0,"Open Output File",4) @@ -263,8 +240,8 @@ Function main() { Write-Host No DistributionGroup found } - #Clean up session - Get-PSSession | Remove-PSSession + #Disconnect Exchange Online session +Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue } . main \ No newline at end of file