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

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

问题描述

我创建一个PowerShell脚本部署过程中的一些code和一部分是调用名为RAR.EXE到备份一些文件夹命令行的COM pression工具。

我试图动态地构建出来的参数,然后有PowerShell的调用与变量的命令,但我遇到麻烦了。这是不工作...

运行下面的脚本,你应该明白我在说什么。的参数被传递作为正在错位的变量。如果我通过整个命令+参数我得到了臭名昭著的不被识别为cmdlet ...消息。

感谢您的帮助!

 回声这应该成功
&安培; CMD / C foo的回声回声为什么会发生这种回声出一个额外的双引号?
$参数=/ C回声富
&安培; CMD$参数回声这样做相同的
$参数=/ C回声富
&安培; CMD $参数回声逃避斜线不工作,要么......
$参数=`/ C回声富
&安培; CMD $参数回声失败,但为什么呢?
$ CMD =CMD / C回声富
&安培; $ CMD


解决方案

呼叫运营商'和;'在这种情况下是不必要的。它是用来在一个新的范围给调用命令。这通常是用于调用由一个字符串或脚本块指定一个命令。它还具有任何变量中说PowerShell脚本创建的命令完成后都将被丢弃一旁效益和范围消失。

然而,由于cmd是在一个完全不同的进程执行的EXE文件。 FWIW,你的cmd.exe直接获得类似的输出:

 > CMD/ C回声富

所以在年底额外的报价是cmd.exe的问题。通常情况下,你需要保持命令从参数分开时,PowerShell是做解析调用命令例如

  45 GT; &安培; {$ foo的=富}
46 GT; $#foo的注意,$ foo的没有被发现 - 它的范围就走了
47> 。 {$ foo的=富}#点缀在当前范围内执行时
48> $ foo的

在这里值得注意的例外是,调用​​-Ex的pression的行为就像一个评估此字符串功能。小心使用,的尤其的如果用户提供的字符串。如果他们提供你一天能吸里C:\\ -r。

在这种情况下,其他人都建议我会拉/ C出字符串参数$字符串,并将其指定例如:

  CMD / C $参数

或者使用调用-Ex的pression但小心使用。顺便说一句,当你尝试调试问题与PowerShell中发送参数EXE,检查出的PowerShell社区扩展的echoargs实用程序(的http:// pscx 。codeplex.com )。这是非常方便的:

  49> $参数=/ C回声富
50 GT; echoargs $参数
精氨酸0为< / C回声富>

这表明,CMD.EXE秉承/ C回声富作为一个的的参数。 / C应该从回声富​​(执行命令)一个单独的参数。

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.

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...

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.

Thanks for any help!

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

解决方案

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.

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"

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

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".

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

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>

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天全站免登陆