我正在尝试列出 Active Directory 域中每个用户的成员资格列表。我创建了以下行:
foreach($_ in $(Get-ADUser -Filter *).Name){
Get-ADPrincipalGroupMembership -Identity $_ | select Name,Groupscope,Groupcategory| sort Name
}
问题是当用户没有任何组成员身份时,运行这行代码会导致出现以下错误。
Get-ADPrincipalGroupMembership : Cannot find an object with identity: 'TEST USER'
under: 'DC=contoso,DC=com'.
添加-Erroraction Silentlycontinue
后面Get-ADPrinicpalGroupMembership
并不能缓解问题。我宁愿不要乱来$ErrorAction
。但是,在生产线完成后更改$ErrorAction
为"silentlycontinue"
和更改回来确实有效。虽然不是一个很好的解决方案。有什么方法可以防止错误显示吗?
noam 解决方案的输出:(仅显示可用组的完整列表,而不是用户的成员资格)
name groupScope groupCategory
---- ---------- -------------
Administrators DomainLocal Security
Distributed COM Users DomainLocal Security
Domain Admins Global Security
Domain Users Global Security
Enterprise Admins Universal Security
Group Policy Creator Ow... Global Security
HelpLibraryUpdaters DomainLocal Security
Schema Admins Universal Security
TESTGROUP1 Global Security
Domain Guests Global Security
Guests DomainLocal Security
Denied RODC Password Re... DomainLocal Security
Domain Users Global Security