我有两个代码块:
$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 中的每个用户调用的,所以标题应该每次都在后一个中显示?