首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >invoke-command不能按预期工作

invoke-command不能按预期工作
EN

Stack Overflow用户
提问于 2013-03-04 18:43:43
回答 2查看 619关注 0票数 2

我正在尝试使用invoke-command来查找使用以下代码的特定进程

代码语言:javascript
复制
Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {Get-Process -Name "winlogon" | where{$_.Id -like $args[0]} }

这个命令不起作用,但是如果我不使用$args[0],而是使用$selected_server.ProcessId中包含的数值8900,它就会起作用。

我还尝试执行这个命令来验证变量是否被正确读取,看起来是正确的

代码语言:javascript
复制
Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {$args[0]; $args[0].gettype().fullname}

> 8900
> System.Int32

我是不是遗漏了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-04 18:58:44

不知道为什么,但这行得通(也许foreach-object scriptblock中的$args就是out of scope):

代码语言:javascript
复制
Invoke-Command -ComputerName $selected_server.ServerName `
-ArgumentList $selected_server.ProcessId -ScriptBlock `
{param ($x) Get-Process -Name "winlogon" | where{$_.Id -like $x} }
票数 3
EN

Stack Overflow用户

发布于 2013-03-04 21:38:28

C.B的答案是好的,只要你有可用的远程处理(v2.0及更高版本),它就可以工作,但是如果你使用的是PowerShell 3.0,还有另一种(更简单)的方法-- Using作用域修饰符。请参阅about_Remote_Variables

代码语言:javascript
复制
Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {Get-Process -Name "winlogon" | where{$_.Id -like  $Using:selected_server.ProcessId} }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15199683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档