Powershell:具有转义参数( - %)的调用运算符(&)不能使用非静态参数 [英] Powershell: Call operator (&) with escape param (--%) not working with non-static args

查看:140
本文介绍了Powershell:具有转义参数( - %)的调用运算符(&)不能使用非静态参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Powershell脚本需要调用一个非常复杂的参数集的EXE。我使用的是Powershell 3.0,并且必须坚持使用该版本。唉,即使是魔法逃跑运营商( - %)并没有帮助我。例如,使用Call运算符,请考虑:

 & other.exe  - %action / mode fast / path:location with spaces/ fancyparam {/ dothis / dothat:arg with spaces} / verbose 

现在,如果这么简单,我的脚本可以很容易地正常工作。但这不是那么简单。 other.exe的参数可能会有所不同,具体取决于我的脚本中的用户选择。因此,我需要提前建立这些参数,也许是这样的:

  $ commandArgs ='action / mode' + $ userMode +'/ path:location with spaces/ fancyparam {/ dothis / dothat:'+ $ userArgs +} / verbose'

因此,我将以这种方式调用:

 & other.exe  - %$ commandArgs 

...好的,期待 - %意味着它只是传递一个原始的字符串 $ commandArgs 。但是如果没有 - %,PowerShell会自动引用$ commandArgs的内容,这真的搞砸了内部引号(更不用说打破前面的action参数那个other.exe首先需要)换句话说,我已经尝试在$ commandArgs字符串中嵌入 - %,但损坏已经在被解析的时候完成了请注意,这个例子只是我需要执行的实际命令的1/4,其中包括更多的用户引用,引用和其他有趣的字符,这将驱使我逃离地狱匆忙!我也已经使用了echoargs.exe工具,这是我看到我遇到的麻烦。哦,我也需要我例子中的所有空格(即需要大括号字符的空格)。



所以经过很多寻找答案,我转向你求助。谢谢提前。

解决方案

好的,可能很奇怪的是回答我自己的问题,但昨天花了一天这个问题后,我可能已经意识到自己的答案。至少,这是我发现的工作。但是我发布在这里得到进一步的反馈,以防万一我真的在做一些不推荐的...使用Invoke-Expression: - )



我有sorta早期实现,有些人在您的答复中确认了这一点, - %阻止所有进一步的扩展(包括我的$变量)。而我的问题是,当尝试使用Call运算符(&)时,我仍然需要扩展很多东西。如果我的命令行在使用 - %之前已经准备好了,我的问题将会解决,所以这就是我所做的。



我创建了一个新的字符串,由以下组成:

  $ fullCommand ='& other.exe - %'+ $ commandArgs 

(EXE路径实际上有空格,因此引用。)然后,它全部建立(包括 - %需要它),我将其作为一个新的脚本调用: / p>

  Invoke-Expression $ fullCommand 

到目前为止,我取得了很好的成绩。但是,在我的搜索中我知道,Invoke-Expression听起来像这样的坏事,人们不应该使用。每个人的想法?


My Powershell script needs to invoke an EXE with a very complicated set of arguments. I'm using Powershell 3.0, and must stick with that version. Alas, even the "magic" escaping operator (--%) isn't helping me. For example, using the Call operator, consider this:

& other.exe --% action /mode fast /path:"location with spaces" /fancyparam { /dothis /dothat:"arg with spaces" } /verbose

Now, if it were that simple, my script could easily work fine. But it isn't that simple. The arguments for "other.exe" can be different, depending on user selections earlier in my script. So instead, I need to build up those parameters ahead of time, perhaps like this:

$commandArgs = 'action /mode ' + $userMode + ' /path:"location with spaces" /fancyparam { /dothis /dothat:"' + $userArgs + " } /verbose'

Thus I would invoke this way:

& other.exe --% $commandArgs

...well, expect that --% means it just passes a raw string of $commandArgs instead. But without the --%, powershell auto-quotes the contents of $commandArgs, which really messes up the internal quotes (not to mention breaking the 'action' argument at the front that other.exe needs first). In other words, I've already tried embedding the --% inside my $commandArgs string, but the damage is already done by the time it would be parsed (and I don't think it even works that way).

NOTE that this example is only about 1/4 of my actual command I need to execute -- which includes many more user args, quotes and other funny characters that would drive me into escaping-hell in a hurry! I've also already been using the echoargs.exe tool, which is how I'm seeing the troubles I'm having. Oh, and I need all the spaces in my example, too (i.e. need spaces around the brace characters).

So after much searching for an answer, I turn to you for help. Thanks in advance.

解决方案

Ok, probably weird to be answering my own question, but after spending another day on this problem yesterday, I might have realized the answer myself. At least, this is what I found that works. But I post here to get further feedback, in case I'm really doing something that isn't recommended ...using Invoke-Expression :-)

I had sorta realized early on, and some of you confirmed this in your responses, that the --% prevents all further expansions (including my $variables). And my problem is that I'm still needing to expand lots of things when trying to use the Call operator (&). My problem would be solved if my command line was all ready to go before using --%, so that's what I did.

I created a new string, composed of:

$fullCommand = '& "other.exe" --% ' + $commandArgs

(The EXE path actually has spaces in it, hence the quotes.) Then, with it all built up (including the --% where it needs to be), I invoke it as a new piece of script:

Invoke-Expression $fullCommand

So far, I'm having very good results. But I know in my search up to this point that Invoke-Expression sounded like this bad thing people shouldn't use. Thoughts, everyone?

这篇关于Powershell:具有转义参数( - %)的调用运算符(&)不能使用非静态参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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