foreach 循环只是不断打印出其中的元素数量,如以下代码所示。我想阻止它打印出来。
$ADSearch = New-Object System.DirectoryServices.DirectorySearcher
$ADSearch.SearchRoot ="LDAP://$Domain"
$ADSearch.SearchScope = "subtree"
$ADSearch.PageSize = 100
$ADSearch.Filter = "(objectClass=$objectClass)"
$properies =@("distinguishedName",
"sAMAccountName",
"mail",
"lastLogonTimeStamp",
"pwdLastSet",
"accountExpires",
"userAccountControl")
foreach($pro in $properies)
{
$ADSearch.PropertiesToLoad.add($pro)
}
目前它给出:
0
1
2
3
4
5
6
更改此:
foreach($pro in $properies)
{
$ADSearch.PropertiesToLoad.add($pro)
}
致:
foreach($pro in $properies)
{
$ADSearch.PropertiesToLoad.add($pro)| out-null
}
另一个解决方案是
[void]
“有问题的”行:
foreach($pro in $properies)
{
[void]$ADSearch.PropertiesToLoad.add($pro)
}