嗨,我在Powershell中遇到了一个问题,即“直到条件”为true,但循环不会停止。如果我将-eq更改为0。它将停止...基本上,这应该做的是获取文本文件中的计算机数。将该数字存储在$ count中。然后,为列表中的每台计算机重新启动服务,直到到达最后一台为止。
$computers = gc C:\temp\computers.txt
$count = $computers.count
Do {
foreach($computer in $computers){
$readCount = $computer.ReadCount
gwmi win32_service -ComputerName $computer | where {$_.name -like "*was*"} | Restart-Service
}
}
Until (($count - $readCount) -eq 1)
您无需在Do-Until
此处循环,因为您可以遍历计算机。若要跳过最后一台计算机,请使用带有以下参数的Select-Object cmdlet -SkipLast 1
:
Get-Content 'C:\temp\computers.txt' | Select-Object -SkipLast 1 | Forach-Object {
gwmi win32_service -ComputerName $computer |
where {$_.name -like "*was*"} |
Restart-Service
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句