如何调用命令并将路径作为参数传递给命令 [英] How to Invoke-Command and pass path to command as parameter

查看:51
本文介绍了如何调用命令并将路径作为参数传递给命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用命令但将路径作为参数传递给exe

I'm tearing my hair out trying to invoke-command but pass the path to the exe as a parameter

例如:我要接这个命令

powershell Invoke-Command -ComputerName localhost -ScriptBlock { param($command ) C:\windows\system32\getmac.exe /$command } -ArgumentList ?

并将其翻译成这样的形式

and translate it into a form like this

powershell Invoke-Command -ComputerName localhost -ScriptBlock { param($path, $command ) $path\getmac.exe /$command } -ArgumentList C:\windows\system32,?

我尝试了各种引用、与符号和其他扭曲方式,但都无法正常工作.上述尝试导致

I've tried all manner of quoting, ampersands and other contortions but can't get it to work. The above attempt results in

表达式或语句中出现意外标记\getmac.exe".在第 1 行字符:97

(我真的不想在 localhost 上调用 getmac,这是可运行的、经过提炼的版本)

(I don't really want to invoke getmac on localhost, this is the runnable, SO distilled version)

推荐答案

试试这个选项.它显示了 cscript.exe 的帮助.

Try this option. It shows me help for cscript.exe.

C:\>powershell.exe Invoke-Command -ComputerName localhost -ScriptBlock { param($path, $command ) cmd /c $path $command } -args '"C:\windows\system32\cscript.exe"','"/?"'

我使用 & 尝试了其他选项,然后使用路径和参数,它给我丢失了 } 异常.然后在脚本块中使用 cmd/c 而不是 & 解决了这个问题.

I tried other options using & and then path and arguments and it was giving me missing } exception. Then using cmd /c instead of & inside scriptblock fixed the issue.

这篇关于如何调用命令并将路径作为参数传递给命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆