Run commands on a Remote Computer
suggest changeOnce PowerShell remoting is enabled (Enable-PSRemoting), you can run commands on the remote computer like this:
Invoke-Command -ComputerName "RemoteComputerName" -ScriptBlock {
Write-Host "Remote Computer Name: $env:COMPUTERNAME"
}
This creates a temporary session and closes it when the script block ends.
To leave the session open and run other commands in it later, create a remote session first:
$Session = New-PSSession -ComputerName "RemoteComputerName"
Then you can use this session each time you invoke commands on the remote computer:
Invoke-Command -Session $Session -ScriptBlock {
Write-Host "Remote Computer Name: $env:COMPUTERNAME"
}
Invoke-Command -Session $Session -ScriptBlock {
Get-Date
}
Pass -Credential when the session is created. Invoke-Command -Session does not accept -Credential.
$Cred = Get-Credential
$Session = New-PSSession -ComputerName "RemoteComputerName" -Credential $Cred
Invoke-Command -Session $Session -ScriptBlock { Get-Date }
Remoting serialization warning
Remoting serializes PowerShell objects on the remote system and deserializes them locally. They are converted to XML during transport and lose their methods. Get-WmiObject in this example is Windows PowerShell only.
$output = Invoke-Command -Session $Session -ScriptBlock {
Get-WmiObject -Class win32_printer
}
$output | Get-Member -MemberType Method
TypeName: Deserialized.System.Management.ManagementObject#root\cimv2\Win32_Printer
Name MemberType Definition
---- ---------- ----------
GetType Method type GetType()
ToString Method string ToString(), string ToString(string format, System.IFormatProvi...
Whereas you have the methods on the regular PS object:
Get-WmiObject -Class win32_printer | Get-Member -MemberType Method
TypeName: System.Management.ManagementObject#root\cimv2\Win32_Printer
Name MemberType Definition
---- ---------- ----------
CancelAllJobs Method System.Management.ManagementBaseObject CancelAllJobs()
GetSecurityDescriptor Method System.Management.ManagementBaseObject GetSecurityDescriptor()
Pause Method System.Management.ManagementBaseObject Pause()
PrintTestPage Method System.Management.ManagementBaseObject PrintTestPage()
RenamePrinter Method System.Management.ManagementBaseObject RenamePrinter(System.String NewPrinterName)
Reset Method System.Management.ManagementBaseObject Reset()
Resume Method System.Management.ManagementBaseObject Resume()
SetDefaultPrinter Method System.Management.ManagementBaseObject SetDefaultPrinter()
SetPowerState Method System.Management.ManagementBaseObject SetPowerState(System.UInt16 PowerState, System.String Time)
SetSecurityDescriptor Method System.Management.ManagementBaseObject SetSecurityDescriptor(System.Management.ManagementObject#Win32_SecurityDescriptor Descriptor)
Argument Usage
To use arguments as parameters for the remote scripting block, one might either use the ArgumentList parameter of Invoke-Command, or use the $Using: syntax.
Using ArgumentList with unnamed parameters (i.e. in the order they are passed to the scriptblock):
$servicesToShow = "service1"
$fileName = "C:\temp\servicestatus.csv"
Invoke-Command -Session $session -ArgumentList $servicesToShow,$fileName -ScriptBlock {
Write-Host "Calling script block remotely with $($Args.Count)"
Get-Service -Name $args[0]
Remove-Item -Path $args[1] -ErrorAction SilentlyContinue -Force
}
Using ArgumentList with named parameters:
$servicesToShow = "service1"
$fileName = "C:\temp\servicestatus.csv"
Invoke-Command -Session $session -ArgumentList $servicesToShow,$fileName -ScriptBlock {
Param($serviceToShowInRemoteSession,$fileToDelete)
Write-Host "Calling script block remotely for $serviceToShowInRemoteSession"
Get-Service -Name $serviceToShowInRemoteSession
Remove-Item -Path $fileToDelete -ErrorAction SilentlyContinue -Force
}
Using $Using: syntax:
$servicesToShow = "service1"
$fileName = "C:\temp\servicestatus.csv"
Invoke-Command -Session $session -ScriptBlock {
Get-Service $Using:servicesToShow
Remove-Item -Path $Using:fileName -ErrorAction SilentlyContinue -Force
}