如何在使用局部变量作为参数的远程计算机上执行脚本块
我想运行一个应用程序,从远程计算机传递参数。我通过在本地运行它来在远程计算机上进行以下操作:
foreach ($a in $args){
&"c:\Program Files\ChristianSteven\CRD\crd.exe" "-s schedulename=Vc_$($a)"
}
我在远程运行它时遇到问题:
foreach ($a in $args){
Invoke-Command -computername $serv -Credential $cred -ScriptBlock {param($b) &"c:\Program Files\ChristianSteven\CRD\crd.exe" $b} -ArgumentList "-s schedulename=Vc_$($a)"
}
从我读到的内容来看,这与变量范围有关,补救措施是在之前创建脚本块您可以使用以下方法将其传递到远程计算机:
[scriptblock]::create(<command>)
我已经尝试了很多组合,但无法让它运行。
I would like to run an application passing it arguments from a remote machine. I've got the following to work on the remote machine by running it locally:
foreach ($a in $args){
&"c:\Program Files\ChristianSteven\CRD\crd.exe" "-s schedulename=Vc_$($a)"
}
I'm having problems running it remotely using:
foreach ($a in $args){
Invoke-Command -computername $serv -Credential $cred -ScriptBlock {param($b) &"c:\Program Files\ChristianSteven\CRD\crd.exe" $b} -ArgumentList "-s schedulename=Vc_$($a)"
}
From what I've read this is to do with the variables scope and the remedy is to create the scriptblock before you pass it to the remote machine using:
[scriptblock]::create(<command>)
I've tried many combinations and I can't get it to run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以这样做:
You can do it like this: