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
相关产品推荐
相关产品推荐

