mardi 26 novembre 2019

PowerShell remove offline machines from a list

I have a script which I want to do a test-connection on a list of machines and then remove the offline machines from the list. This is what I have:

[System.Collections.ArrayList]$Systems = Get-Content -Path C:\temp\test.txt

Foreach ($System in $Systems){

if ($false -eq (Test-Connection -CN $System -Count 1 -Quiet))

 {

 $Systems.Remove("$System")

 } 

 }

 $Systems ```

This fails with this error:

Collection was modified; enumeration operation may not execute. At C:\temp\test.ps1:3 char:10 + Foreach ($System in $Systems){ + ~~~~~~~ + CategoryInfo : OperationStopped: (:) [], InvalidOperationException + FullyQualifiedErrorId : System.InvalidOperationException

Any advice please?

Aucun commentaire:

Enregistrer un commentaire