从输出列表中排除服务器

时间:2016-08-09 20:02:21

标签: powershell

我有一个脚本报告来自整个林的NTDS服务状态,我想在下面排除的2003服务器就是脚本。

$getForest = [system.directoryservices.activedirectory.Forest]::GetCurrentForest()
$getForest.domains | ForEach-Object {$_.DomainControllers} | ForEach-Object {$_.Name} 

我正在尝试像

这样的东西
$getForest = [system.directoryservices.activedirectory.Forest]::GetCurrentForest()
$getForest.domains | ForEach-Object {$_.DomainControllers} | ForEach-Object {$_.Name} | where-object {$_.name -notlike "server2003.domain.local"} 

没有运气

2 个答案:

答案 0 :(得分:1)

不需要你的第二个ForEach-Object,所以我把它整合到了第一个。{/ p>

对于这种事情,我建议创建一个你想要排除的名字数组,我称之为$exclude

然后我使用-notcontains运算符根据Where-Object中的列表检查名称。

$exclude = @(
    'server2003.domain.local'
    'other2003.domain.local'
)
$getForest = [system.directoryservices.activedirectory.Forest]::GetCurrentForest()
$getForest.domains | 
    ForEach-Object {$_.DomainControllers.Name} | 
    Where-Object {$exclude -notcontains $_.name} 

答案 1 :(得分:0)

我得到了它的工作

true
相关问题