Search code examples
powershellinvoke-command

invoke-command doesn't work as expected


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?


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} }