Difference between revisions of "Invoke-Command"

From UVOO Tech Wiki
Jump to navigation Jump to search
Line 3: Line 3:
 
curl.exe -L $URL -o C:\tmp\myscript.ps1
 
curl.exe -L $URL -o C:\tmp\myscript.ps1
  
Invoke-Command -ComputerName $hostname -f C:\tmp\myscript.ps1
+
$hostname = "myhost"; Invoke-Command -ComputerName $hostname -f C:\tmp\myscript.ps1
  
Invoke-Command -ComputerName $host -ScriptBlock { Get-ChildItem C:\ } -credential $username
+
Invoke-Command -ComputerName $hostname -ScriptBlock { Get-ChildItem C:\ } -credential $username
  
Invoke-Command -ComputerName $host-f C:\tmp\local-file-on-remote.ps1
+
Invoke-Command -ComputerName $hostname -f C:\tmp\local-file-on-remote.ps1
 
```
 
```
  

Revision as of 16:45, 23 January 2022

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 }
}