Powershell run commands on remote servers
Last updated:
POWERSHELL
Use Invoke-Command to run one off commands, if no output is needed.
$Servers | Invoke-Command -ScriptBlock { Get-Service }
Invoke-command can be used with New-PSSession. This can be useful when running multiple commands and you want to use the same session. Like so:
$s = New-PSSession -ComputerName Server02
Invoke-Command -Session $s -ScriptBlock {$p = Get-Process PowerShell}
Invoke-Command -Session $s -ScriptBlock {$p.VirtualMemorySize}
references: Invoke-Command (Microsoft.PowerShell.Core) - PowerShell | Microsoft Docs