停止在foreach循环powershell中打印元素的数量

问题描述 投票:0回答:2

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
powershell foreach
2个回答
5
投票

更改此:

foreach($pro in $properies)
{
    $ADSearch.PropertiesToLoad.add($pro)   
}

致:

foreach($pro in $properies)
{
    $ADSearch.PropertiesToLoad.add($pro)| out-null
}

4
投票

另一个解决方案是

[void]
“有问题的”行:

foreach($pro in $properies)
{
    [void]$ADSearch.PropertiesToLoad.add($pro)
}
© www.soinside.com 2019 - 2024. All rights reserved.