FredBainbridge

Invoke-Command Example

I use this CmdLet all the time. It is really the bees knees as far as remote management of devices go. Here is an example to use as reference.

$ComputerName = "SERVER01"
$RemoteSession = New-PSSession -ComputerName $ComputerName

#return a scalar value
$results = Invoke-Command -Session $remoteSession -ScriptBlock {
    param($variablePassedToRemoteSession)
    $output = get-process $variablePassedToRemoteSession
    Write-output $output
 } -ArgumentList $variablePassedIn

$results | Get-Member
 #return a more complex object

$results = Invoke-Command -Session $remoteSession -ScriptBlock {
   param($variablePassedToRemoteSession)   
   $process = Get-Process $variablePassedToRemoteSession
   Write-Output "Handles", $process.Handles
   Write-Output "PeakWorkingSet", $process.PeakWorkingSet

} -ArgumentList $variablePassedIn

$results 

Share