Hi I have an issue in powershell where the Do Until Condition is true, but the loop doesn't stop. If I change the -eq to 0. It will stop... Basically what this should do is get the number of computers in the text file. Store that number in $count. Then restart the service for each computer in the list until it reaches the last one.
$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)
You don't need a Do-Until
loop here since you can just iterate over the computers. To skip the last computer, use the Select-Object cmdlet with the -SkipLast 1
parameter:
Get-Content 'C:\temp\computers.txt' | Select-Object -SkipLast 1 | Forach-Object {
gwmi win32_service -ComputerName $computer |
where {$_.name -like "*was*"} |
Restart-Service
}