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

查看:34
本文介绍了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"

所以最后的额外引用是一个 cmd.exe 问题.通常,当 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 的行为类似于评估此字符串"函数.小心使用,尤其,如果用户提供了字符串.如果他们提供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 字符串中拉出并指定它,例如:

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

或者使用 Invoke-Expression 但要小心使用.顺便说一句,当您尝试调试从 PowerShell 向 EXE 发送参数的问题时,请查看 PowerShell 社区扩展中的 echoargs 实用程序 (http://pscx.codeplex.com).非常好用:

Or use Invoke-Expression but use with care. BTW when you are trying to debug issues with sending arguments to EXE from PowerShell, check out the echoargs utility in PowerShell Community Extensions (http://pscx.codeplex.com). It is very handy:

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

这表明 cmd.exe 接收/c echo foo"作为单个参数./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天全站免登陆