我有两个代码块:
$allUsers = Get-ADUser -Filter *
foreach($_ in $allUsers.Name){
Write-Host "User $_ is member of the following groups:" -for Yellow
Get-ADPrincipalGroupMembership -Identity $_ | select Name,objectClass,Groupscope,Groupcategory | sort Name | Format-Table
}
User Administrator is member of the following groups:
Name objectClass Groupscope Groupcategory
---- ----------- ---------- -------------
Administrators group DomainLocal Security
Distributed COM U... group DomainLocal Security
Domain Admins group Global Security
Domain Users group Global Security
Enterprise Admins group Universal Security
Group Policy Crea... group Global Security
HelpLibraryUpdaters group DomainLocal Security
Schema Admins group Universal Security
User Guest is member of the following groups:
Name objectClass Groupscope Groupcategory
---- ----------- ---------- -------------
Domain Guests group Global Security
Guests group DomainLocal Security
另一个版本如下:
$allUsers = Get-ADUser -Filter *
foreach($_ in $allUsers.Name){
Write-Host "User $_ is member of the following groups:"
Get-ADPrincipalGroupMembership -Identity $_ | select Name,objectClass,Groupscope,Groupcategory | sort Name
}
User Administrator is member of the following groups:
Name objectClass Groupscope Groupcategory
---- ----------- ---------- -------------
Administrators group DomainLocal Security
Distributed COM U... group DomainLocal Security
Domain Admins group Global Security
Domain Users group Global Security
Enterprise Admins group Universal Security
Group Policy Crea... group Global Security
HelpLibraryUpdaters group DomainLocal Security
Schema Admins group Universal Security
User Guest is member of the following groups:
Domain Guests group Global Security
Guests group DomainLocal Security
在我看来,后者更容易阅读。我的问题是这是;为什么第一个代码块显示每个用户的标题?我看到它的方式,为$ allUsers.Name中的每个用户调用select-object,所以每次在后者中都应该显示标题?
答案 0 :(得分:0)
差异是调用Format-Table
的次数。在第一个示例中,为每个用户调用它。这意味着为每个用户打印标题。在第二个示例中,Format-Table
仅被调用一次,因此标题只打印一次。
第二个命令最后有一个隐含的Format-Table
,所以如果你这样做:
$allUsers = Get-ADUser -Filter *
foreach($_ in $allUsers.Name){
Write-Host "User $_ is member of the following groups:"
Get-ADPrincipalGroupMembership -Identity $_ | select Name,objectClass,Groupscope,Groupcategory | sort Name
} | Format-Table
你应该得到相同的结果。