From fa3278fec17e4031c7122f54734882f502932cf5 Mon Sep 17 00:00:00 2001 From: AdminDroid <49208841+admindroid-community@users.noreply.github.com> Date: Wed, 27 Apr 2022 18:41:03 +0530 Subject: [PATCH] Get DLs a User is Member Of Get DLs a User is Member Of --- .../GetDistributionGroups_UserIsMemberOf.ps1 | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 Get DLs a User is Member Of/GetDistributionGroups_UserIsMemberOf.ps1 diff --git a/Get DLs a User is Member Of/GetDistributionGroups_UserIsMemberOf.ps1 b/Get DLs a User is Member Of/GetDistributionGroups_UserIsMemberOf.ps1 new file mode 100644 index 0000000..47b364e --- /dev/null +++ b/Get DLs a User is Member Of/GetDistributionGroups_UserIsMemberOf.ps1 @@ -0,0 +1,150 @@ +<# +============================================================================================= +Name: Export Distrbution Groups a user is member of +Description: This script exports all users and their distribution group membership +Website: o365reports.com +Script by: o365reports Team +For detailed script execution: https://o365reports.com/2022/04/19/list-all-the-distribution-groups-a-user-is-member-of-using-powershell/ +============================================================================================ +#> +Param +( + [string]$UserName=$Null, + [string]$Password=$Null, + [string]$UserPrincipalName=$Null, + [string]$InputCsvFilePath=$Null +) +Function Connect_Exo +{ + #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 + } + Else + { + Write-Host EXO V2 module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet. + Exit + } + } + #Importing Module by default will avoid the cmdlet unrecognized error + Import-Module ExchangeOnlineManagement + 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 "")) + { + $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force + $Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword + Connect-ExchangeOnline -Credential $Credential + } + Else + { + Connect-ExchangeOnline + } +} + +Function Import_Csv +{ + #Importing UserPrincipalName From The Csv + Try + { + $UserDetails=@() + Write-Host "Importing UserPrincipalNames from Csv..." + Import-Csv -LiteralPath $InputCsvFilePath|ForEach{ + $UserPrincipalName=$_.UserPrincipalName + Try + { + Get-Mailbox -Identity $UserPrincipalName -ResultSize unlimited -ErrorAction Stop | ForEach{ + List_DLs_That_User_Is_A_Member + } + } + Catch + { + Write-Host "$UserPrincipalName is not a valid user" + } + } + + } + catch + { + Write-Host "$InputCsvFilePath is not a valid file path" + } +} +Function List_DLs_That_User_Is_A_Member +{ + #Finding Distribution List that User is a Member + $Result= @() + $DistinguishedName=$_.DistinguishedName + $Filter = "Members -Like ""$DistinguishedName""" + $UserPrincipalName=$_.UserPrincipalName + $UserDisplayName=$_.DisplayName + Write-Progress -Activity "Find Distribution Lists that user is a member" -Status "Processed User Count: $Global:ProcessedUserCount" -CurrentOperation "Currently Processing in $UserPrincipalName" + $DLs=Get-DistributionGroup -ResultSize Unlimited -Filter $Filter + $GroupCount=$DLs | Measure-Object | select count + If($GroupCount.count -ne 0) + { + $DLsCount=$GroupCount.count + $DLsName=$DLs.Name + $DLsEmailAddress=$DLs.PrimarySmtpAddress + } + Else + { + $DLsName="-" + $DlsEmailAddress="-" + $DLsCount='0' + } + $Result=New-Object PsObject -Property @{'User Principal Name'=$UserPrincipalName;'User Display Name'=$UserDisplayName;'No of DLs that user is a member'=$DLsCount;'DLs Name'=$DLsName -join ',';'DLs Email Adddress'=$DLsEmailAddress -join ',';} + $Result|Select-Object 'User Principal Name','User Display Name','No Of DLs That User Is A Member','DLs Name','DLs Email Adddress'| Export-Csv $OutputCsv -NoTypeInformatio -Append + $Global:ProcessedUserCount++ + +} +Function OpenOutputCsv +{ + #Open Output File After Execution + If((Test-Path $OutputCsv) -eq "True") + { + Write-Host "The Output file available in $OutputCsv" + $Prompt = New-Object -ComObject wscript.shell + $UserInput = $Prompt.popup("Do you want to open output file?",` 0,"open output file",4) + If($UserInput -eq 6) + { + Invoke-Item "$OutputCsv" + } + } +} +Connect_Exo +$Global:ProcessedUserCount=0 +$OutputCsv=".\ListDLs_UsersIsMemberOf_$((Get-Date -format MMM-dd` hh-mm` tt).ToString()).csv" +If($UserPrincipalName -ne "") +{ + Try + { + write-Host "Checking $UserPrincipalName is a valid user or not" + Get-Mailbox -Identity $UserPrincipalName -ErrorAction Stop|ForEach{ + List_DLs_That_User_Is_A_Member + } + } + Catch + { + Write-Host "$UserPrincipalName is not a valid user" + } +} +Elseif($InputCsvFilePath -ne "") +{ + Import_Csv +} +Else +{ + Get-Mailbox -ResultSize unlimited -RecipientTypeDetails UserMailbox | ForEach{ + List_DLs_That_User_Is_A_Member + } +} +OpenOutputCsv +#Removing connected session +Get-PSSession |Remove-PSSession \ No newline at end of file