Difference between revisions of "Invoke-Command"

From UVOO Tech Wiki
Jump to navigation Jump to search
(Created page with "Running command or local script on remote host ``` Invoke-Command -ComputerName $host -ScriptBlock { Get-ChildItem C:\ } -credential $username Invoke-Command -ComputerName $h...")
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
```
 +
$regex = "foo"
 +
foreach($line in Get-Content .\hosts.txt) {
 +
    if($line -match $regex){
 +
        write-output $line
 +
        Invoke-Command -ComputerName "$line" -ScriptBlock { hostname }
 +
    }
 +
}
 +
```
 +
 +
```
 +
foreach($line in Get-Content .\hosts.txt) {
 +
    write-output $line
 +
    Invoke-Command -ComputerName "$line" -ScriptBlock { sc.exe failure "my service" reset=36000 actions=restart/90000 }
 +
}
 +
```
 +
https://stackoverflow.com/questions/33511772/read-file-line-by-line-in-powershell
 +
 +
 +
 +
Disk space
 +
```
 +
invoke-command -scriptblock {Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'FreeSpace (GB)'; Expression= { [int]($_.FreeSpace / 1GB) }}} <myhost>
 +
```
 +
 
Running command or local script on remote host
 
Running command or local script on remote host
 
```
 
```
Invoke-Command -ComputerName $host -ScriptBlock { Get-ChildItem C:\ } -credential $username
+
curl.exe -L $URL -o C:\tmp\myscript.ps1
 +
 
 +
$hostname = "myhost"; Invoke-Command -ComputerName $hostname -f C:\tmp\myscript.ps1
 +
 
 +
Invoke-Command -ComputerName $hostname -ScriptBlock { Get-ChildItem C:\ } -credential $username
 +
 
 +
Invoke-Command -ComputerName $hostname -f C:\tmp\local-file-on-remote.ps1
 +
```
 +
 
 +
Run on array of multiple hosts via foreach
 +
```
 +
$hostnames = @(
 +
    'host1'
 +
    'host2'
 +
)
 +
 
 +
foreach ($hostname in $hostnames) {
 +
  Invoke-Command -ComputerName $hostname -ScriptBlock { ipconfig }
 +
}
 +
```
 +
 
 +
# DNS
 +
```
 +
$ErrorActionPreference = "Stop"
  
Invoke-Command -ComputerName $host-f C:\tmp\local-file-on-remote.ps1
+
$rsp = Invoke-Command -ComputerName wjp1-dc -ScriptBlock {
 +
  $zoneName = "example.com"
 +
  $dnsRecords = Get-DnsServerResourceRecord -ZoneName $zoneName -RRType A
 +
  foreach ($record in $dnsRecords) {
 +
    Write-Output "$($record.HostName).$zoneName"
 +
  }
 +
  $dnsRecords = Get-DnsServerResourceRecord -ZoneName $zoneName -RRType CName
 +
  foreach ($record in $dnsRecords) {
 +
    Write-Output "$($record.HostName).$zoneName"
 +
  }
 +
}
 +
write-output "$rsp" > rsp.txt
 
```
 
```

Latest revision as of 03:03, 19 January 2024

$regex = "foo"
foreach($line in Get-Content .\hosts.txt) {
    if($line -match $regex){
        write-output $line
        Invoke-Command -ComputerName "$line" -ScriptBlock { hostname }
    }
}
foreach($line in Get-Content .\hosts.txt) {
    write-output $line
    Invoke-Command -ComputerName "$line" -ScriptBlock { sc.exe failure "my service" reset=36000 actions=restart/90000 }
}

https://stackoverflow.com/questions/33511772/read-file-line-by-line-in-powershell

Disk space

invoke-command -scriptblock {Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'FreeSpace (GB)'; Expression= { [int]($_.FreeSpace / 1GB) }}} <myhost>

Running command or local script on remote host

curl.exe -L $URL -o C:\tmp\myscript.ps1

$hostname = "myhost"; Invoke-Command -ComputerName $hostname -f C:\tmp\myscript.ps1

Invoke-Command -ComputerName $hostname -ScriptBlock { Get-ChildItem C:\ } -credential $username

Invoke-Command -ComputerName $hostname -f C:\tmp\local-file-on-remote.ps1

Run on array of multiple hosts via foreach

$hostnames = @(
    'host1'
    'host2'
)

foreach ($hostname in $hostnames) {
  Invoke-Command -ComputerName $hostname -ScriptBlock { ipconfig }
}

DNS

$ErrorActionPreference = "Stop"

$rsp = Invoke-Command -ComputerName wjp1-dc -ScriptBlock {
  $zoneName = "example.com"
  $dnsRecords = Get-DnsServerResourceRecord -ZoneName $zoneName -RRType A
  foreach ($record in $dnsRecords) {
    Write-Output "$($record.HostName).$zoneName"
  }
  $dnsRecords = Get-DnsServerResourceRecord -ZoneName $zoneName -RRType CName
  foreach ($record in $dnsRecords) {
    Write-Output "$($record.HostName).$zoneName"
  }
}
write-output "$rsp" > rsp.txt