【问题标题】:Find if a user is part of some distribution lists查找用户是否是某些通讯组列表的一部分
【发布时间】:2023-02-10 01:04:47
【问题描述】:

我想让一个脚本在 powershell 中工作,它获取用户的电子邮件并根据几个分发列表查找它以查看用户是否是其中任何一个的一部分。它还应检查嵌套通讯组如果在主要分发列表下有的话。

这是我所拥有的,但无法正常工作。任何帮助将不胜感激,我对此很陌生。

# 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)"
        }
    }
}

但我在执行时遇到以下错误:

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

【问题讨论】:

    标签: powershell nested distribution-list


    【解决方案1】:

    错误语句告诉我们“-Identify $DL”为空; $DL 返回整个对象行,而不仅仅是要匹配的名称。要更正,请重构为 $DL.DistributionLists,其中“DistributionLists”是导入的 CSV 文件中的列标题。

    正如我们一起确认的那样,您已经导入了 ExchangeOnlineManagement 并连接到 Exchange Online...。我将这些保留在下面的代码中以供将来的读者参考。

    # 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)"
            }
        }
    }
    

    【讨论】:

    • 我已连接到在线交换并拥有正确的 csv 文件。我仍然创建了一个新的 csv 文件并重新连接到 exO 以确保,但同样的错误。
    • 您是否确定 DLs.csv 文件中的名称列包含通讯组列表的正确标识值?您可能希望向脚本中添加一些代码以帮助识别问题。建议您在 Get-DistributionGroupMember 之前添加以下行: Write-Output "Checking $($DL.Name)" 这会将每个分发列表的名称写入输出,以便您可以确定是否从 DL 中读取了正确的值.csv 文件。
    • 添加了建议的行:# Get List of Distribution Group Members Write-Output "Checking $($DL.Name)" $GroupMembers = Get-DistributionGroupMember -Identity $DL -ResultSize Unlimited csv looks like: DistributionLists dl1-name dl2-name dl3 -name 输出:出现相同的错误,但现在每个错误前面都有“正在检查”一词。
    • 在我上面的问题末尾添加了错误截图。
    • #identity 正在返回整行 - 需要只获取名称-DistributionGroupMember -Identity $DL.DistributionLists -ResultSize Unlimited
    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 2012-10-08
    • 2011-12-19
    • 1970-01-01
    相关资源
    最近更新 更多