You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell调用Get-DistributionGroup部分属性报错问题求助

问题根因

你遇到的报错是PowerShell属性引用语法错误导致的:
PowerShell 中访问管道当前遍历对象$_的属性,正确语法为$_.属性名,你脚本中将.和_的顺序写反,错误写成了$._属性名:PowerShell 会将$._识别为不存在的变量,后续的ManagedBy、MemberJoinRestriction等字符串会被判定为要执行的命令,因此抛出「未识别为cmdlet/函数名称」的报错。
你单独执行单属性查询时使用了正确的语法,因此没有报错。

修正后的脚本

针对报错点修正语法后即可正常运行,额外针对多值属性ManagedBy做了字符串拼接优化,避免CSV导出时只显示类型名:

$Groups = Get-DistributionGroup –ResultSize Unlimited
$Groups | ForEach-Object {
    $group = $_.Name
    $primarySMTPAddress = $_.PrimarySMTPAddress
    $managedby = $_.ManagedBy -join ';'
    $memberjoinrestriction = $_.MemberJoinRestriction
    $memberdepartrestriction = $_.MemberDepartRestriction
    $requiresenderauthenticationenabled = $_.RequireSenderAuthenticationEnabled

    Get-DistributionGroupMember $group | ForEach-Object {
        New-Object -TypeName PSObject -Property @{
            Group = $group
            GroupPrimarySMTPAddress = $primarySMTPAddress
            Member = $_.Name
            ManagedBy = $managedby
            MemberJoinRestriction = $memberjoinrestriction
            MemberDepartRestriction = $memberdepartrestriction
            RequireSenderAuthenticationEnabled = $requiresenderauthenticationenabled
            EmailAddress = $_.PrimarySMTPAddress
        }
    }
} | Export-CSV "C:\DistributionGroupinfov1.csv" -NoTypeInformation -Encoding UTF8

内容的提问来源于stack exchange,提问作者Bernke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 02:12:00