Search code examples
powershellnesteddistribution-list

Find if a user is part of some distribution lists


I want to get a script working in powershell which takes a user's email and look it up against a few distribution lists to see if the user is a part of any of them. It should also check the nested distribution groups if any under the main distribution lists.

here's what I have but can't get it to work. Any help would be appreciated, I am fairly new to this.

# Prompt for user email address
$UserEmail = Read-Host -Prompt 'Please enter the user email address'

# Read the CSV file
$DistributionLists = Import-Csv -Path '.\DLs.csv'

# Loop through each Distribution List
foreach ($DL in $DistributionLists) {
    # Get List of Distribution Group Members
    $GroupMembers = Get-DistributionGroupMember -Identity $DL -ResultSize Unlimited

    # Loop through each member
    foreach ($Member in $GroupMembers) {
        # Check if the user's email address matches
        if ($Member.PrimarySmtpAddress -eq $UserEmail) {
            # Output the matches
            Write-Output "User $UserEmail is a part of $($DL.Name)"
        }
    }
}

but i get below error on execution:

Write-ErrorMessage : Cannot process argument transformation on parameter 'Identity'. Cannot convert value "" to type
"Microsoft.Exchange.Configuration.Tasks.DistributionGroupMemberIdParameter". Error: "Parameter values of type Microsoft.Exchange.Configuration.Tasks.DistributionGroupMemberIdParameter can't be empty. Specify a value, and try again.
Parameter name: identity"
At C:\Users\abcd\AppData\Local\Temp\tmpA_hrt0empv.vlz\tmpA_hrt0empv.vlz.psm1:1087 char:13
+             Write-ErrorMessage $ErrorObject
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-DistributionGroupMember], ParameterTransformationException
    + FullyQualifiedErrorId : [Server=BNxxxxxx5601,RequestId=abcdef5-1e51-d5f0-2a56-77b30f23bf3a,TimeStamp=Thu, 09 Feb 2023 14:04:01 GMT],Write-ErrorMessage

Error screenshot


Solution

  • The error statement informs us "-Identify $DL" is empty; $DL returns the entire object row and not just the name to be matched. To correct, refactor to $DL.DistributionLists where "DistributionLists" is the column header in the imported CSV file.

    As we confirmed together you have already imported ExchangeOnlineManagement and made the connected to Exchange Online.... I've kept these in the code below for future reader reference.

    # Pearl-script below:
    # Import the ExchangeOnlineManagement module
    Import-Module ExchangeOnlineManagement
    
    # Connect to Exchange Online
    Connect-ExchangeOnline
    
    # Prompt for user email address
    $UserEmail = Read-Host -Prompt 'Please enter the user email address'
    
    # Read the CSV file
    $DistributionLists = Import-Csv -Path '.\DLs.csv'
    
    # Loop through each Distribution List
    foreach ($DL in $DistributionLists) {
        # Get List of Distribution Group Members
        $GroupMembers = Get-DistributionGroupMember -Identity $DL.DistributionLists -ResultSize Unlimited
    
        # Loop through each member
        foreach ($Member in $GroupMembers) {
            # Check if the user's email address matches
            if ($Member.PrimarySmtpAddress -eq $UserEmail) {
                # Output the matches
                Write-Output "User $UserEmail is a part of $($DL.DistributionLists)"
            }
        }
    }