Question

I'm trying to use invoke-command to find a specific process using this code

Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {Get-Process -Name "winlogon" | where{$_.Id -like $args[0]} }

This command doesn't work, but if I use the numeric value contained in $selected_server.ProcessId that is 8900, instead of using $args[0], it works.

I also tried to execute this command to verify if variables are read correctly and it seems so

Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {$args[0]; $args[0].gettype().fullname}

> 8900
> System.Int32

Am I missing something?

Was it helpful?

Solution

Don't know why but this works ( maybe $args in foreach-object scriptblock is out of scope):

Invoke-Command -ComputerName $selected_server.ServerName `
-ArgumentList $selected_server.ProcessId -ScriptBlock `
{param ($x) Get-Process -Name "winlogon" | where{$_.Id -like $x} }

OTHER TIPS

C.B's answer is good & works anywhere you have remoting available (v2.0 & higher), but there is another (easier) way if you're using PowerShell 3.0 - the Using scope modifier. See about_Remote_Variables

Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {Get-Process -Name "winlogon" | where{$_.Id -like  $Using:selected_server.ProcessId} }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top