我正在尝试使用 invoke-command 来查找使用此代码的特定进程
Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {Get-Process -Name "winlogon" | where{$_.Id -like $args[0]} }
这个命令不起作用,但是如果我使用包含在
$selected_server.ProcessId
即 8900,而不是使用 $args[0]
,它可以工作。
我还尝试执行此命令来验证变量是否正确读取,看起来是这样
Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {$args[0]; $args[0].gettype().fullname}
> 8900
> System.Int32
我错过了什么吗?
最佳答案
不知道为什么,但这有效(也许foreach-object scriptblock
中的$args
超出范围
):
Invoke-Command -ComputerName $selected_server.ServerName `
-ArgumentList $selected_server.ProcessId -ScriptBlock `
{param ($x) Get-Process -Name "winlogon" | where{$_.Id -like $x} }
关于powershell - 调用命令未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15199683/