Powershell命令处理(传入变量) [英] Powershell Command Processing (Passing in Variables)

查看:350
本文介绍了Powershell命令处理(传入变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Powershell脚本来部署一些代码,并且部分过程是调用名为RAR.EXE的命令行压缩工具来备份一些文件夹。

I'm creating a Powershell script to deploy some code and part of the process is to call a command-line compression tool called RAR.EXE to back-up some folders.

我试图动态建立参数,然后有powershell调用命令与变量,但我遇到麻烦。它不工作...

I'm attempting to dynamically build out the parameters and then have powershell call the command with the variables but I'm running into trouble. It isn't working...

运行以下脚本,你应该看到我在说什么。作为变量传递的参数正在被破坏。如果我传递整个命令+参数,我得到臭名昭着的不被识别为cmdlet ...消息。

Run the following script and you should see what I'm talking about. The parameters being passed in as a variable are being mangled. If I pass the entire command + parameters in I get the infamous "is not recognized as a cmdlet..." message.

感谢任何帮助!

echo "this should succeed"
& cmd /c echo foo

echo "why does this echo out an additional double quote?"
$param = "/c echo foo"
& cmd "$param"

echo "this does the same"
$param = "/c echo foo"
& cmd $param

echo "escaping the slash doesn't work either..."
$param = "`/c echo foo"
& cmd $param

echo "this fails, but why?"
$cmd = "cmd /c echo foo"
&$cmd


推荐答案

在这种情况下,调用操作符'&'是不必要的。它用于在新作用域中调用命令。这通常用于调用由字符串或脚本块指定的命令。它还有一个好处是,在PowerShell脚本中创建的任何变量都会在命令完成后被删除,范围消失。

The call operator '&' is unnecessary in this case. It is used to invoke a command in a new scope. This is typically used to invoke a command specified by a string or scriptblock. It also has the side benefit that any variables created in say a PowerShell script are discarded after the command finishes and the scope goes away.

但是由于cmd是一个EXE在完全不同的过程中执行。 FWIW,你直接从cmd.exe得到类似的输出:

However since the cmd is an EXE it executes in a completely different process. FWIW, you get similar output directly from cmd.exe:

> cmd "/c echo foo"
foo"

通常你需要保持命令独立于参数,当PowerShell正在解析以调用命令例如

So the extra quote on the end is a cmd.exe issue. Typically you need to keep the command separate from the parameters when PowerShell is doing the parsing to invoke the command e.g.

45> & { $foo = "foo" }
46> $foo  # Note that $foo wasn't found - it went away with the scope
47> . { $foo = "foo" } # dotting executes in the current scope
48> $foo 
foo

这里值得注意的例外是Invoke-Expression像evaluate this string函数一样使用,请小心,特别是如果用户提供字符串,你的日子可能会吸收,如果他们提供ri C :\ -r。

The notable exception here is that Invoke-Expression behaves like an "evaluate this string" function. Use with care, especially if the user provides the string. Your day could suck if they provided "ri C:\ -r".

在这种情况下,正如其他人建议的那样,我将/ c从字符串$ param string中拉出来, p>

In this case, as others have suggested I would pull the /c out of the string $param string and specify it e.g.:

cmd /c $param


b $ b

或者使用Invoke-Expression,但要小心使用。当你试图调试从PowerShell向EXE发送参数的问题时,请检查PowerShell社区扩展中的echoargs实用程序( http://pscx.codeplex.com )。非常方便:

49> $param = "/c echo foo"
50> echoargs $param
Arg 0 is </c echo foo>

这表明cmd.exe接收/ c echo foo em>参数。 / c应该是与echo foo(要执行的命令)分开的参数。

This shows that cmd.exe receives "/c echo foo" as a single argument. "/c" should be a separate argument from "echo foo" (the command to execute).

这篇关于Powershell命令处理(传入变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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