I'm trying to use invoke-command to find a specific process using this code
我正在尝试使用invoke-command来查找使用此代码的特定进程
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.
这个命令不起作用,但是如果我使用$selected_server中包含的数值。ProcessId是8900,而不是使用$args[0],它是有效的。
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?
我遗漏了什么东西?
2 个解决方案
#1
3
Don't know why but this works ( maybe $args
in foreach-object scriptblock
is out of scope
):
不知道为什么会这样(也许foreacher -object scriptblock中的$args不在范围内):
Invoke-Command -ComputerName $selected_server.ServerName `
-ArgumentList $selected_server.ProcessId -ScriptBlock `
{param ($x) Get-Process -Name "winlogon" | where{$_.Id -like $x} }
#2
2
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
C。B的答案是好的,可以在任何有远程访问可用的地方(v2.0或更高版本)工作,但是如果您使用PowerShell 3.0——使用范围修饰符——还有另一种(更简单的)方法。看到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} }
#1
3
Don't know why but this works ( maybe $args
in foreach-object scriptblock
is out of scope
):
不知道为什么会这样(也许foreacher -object scriptblock中的$args不在范围内):
Invoke-Command -ComputerName $selected_server.ServerName `
-ArgumentList $selected_server.ProcessId -ScriptBlock `
{param ($x) Get-Process -Name "winlogon" | where{$_.Id -like $x} }
#2
2
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
C。B的答案是好的,可以在任何有远程访问可用的地方(v2.0或更高版本)工作,但是如果您使用PowerShell 3.0——使用范围修饰符——还有另一种(更简单的)方法。看到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} }