Invoke-Command may be used with a PS session to execute a command or script on a remote system:
$session = New-PSSession -ComputerName $env:COMPUTERNAME Invoke-Command { Get-Process -Id $PID } -Session $session
$env:COMPUTERNAME is localhost:
Connecting to a session requires administrative access by default. The preceding command will fail if PowerShell is not running with an administrative token (run as administrator).
A PowerShell session with the administrator token can be started using the following command:
Start-Process powershell -Verb RunAs
Connecting to a session requires administrative access by default. The preceding command will fail if PowerShell is not running with an administrative token (run as administrator).
A PowerShell session with the administrator token can be started using the following command:
Start-Process powershell -Verb RunAs
Invoke-Command has a wide variety of different uses, as shown in the command help. For example, a single command can be executed against a list of computers:
Invoke-Command { Get-Process -Id $PID } -ComputerName 'first', 'second', 'third'
This technique can be useful when combined with AsJob. Pushing the requests into the background allows each server to get on with its work, pushing it back when the work is complete.
Once the job created by the previous command has completed, any data may be retrieved using the Receive-Job command.
A number of advanced techniques may be used with Invoke-Command.