Difference between revisions of "Foreach"

From UVOO Tech Wiki
Jump to navigation Jump to search
(Created page with "hosts.txt ``` host1.foo host2.foo ``` $regex = "foo" foreach($line in Get-Content .\hosts.txt) { if($line -match $regex){ write-output $line Invoke-Comman...")
 
 
Line 5: Line 5:
 
```
 
```
  
 +
foo.ps1
 +
```
 
$regex = "foo"
 
$regex = "foo"
 
foreach($line in Get-Content .\hosts.txt) {
 
foreach($line in Get-Content .\hosts.txt) {
Line 12: Line 14:
 
     }
 
     }
 
}
 
}
 +
```

Latest revision as of 15:16, 25 April 2022

hosts.txt

host1.foo
host2.foo

foo.ps1

$regex = "foo"
foreach($line in Get-Content .\hosts.txt) {
    if($line -match $regex){
        write-output $line
        Invoke-Command -ComputerName "$line" -ScriptBlock { hostname }
    }
}