将string []从批处理文件(包含双引号“)传递到powershell脚本 [英] Passing string[] from batch file (which contains double quotes ") to powershell script

查看:131
本文介绍了将string []从批处理文件(包含双引号“)传递到powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有 string [] 参数的powershell脚本,并尝试从批处理文件中将值传递给它。

I have a powershell script which has a string[] parameter and try to pass value to this from batch file

PowerShell脚本

[string[]]$ScriptArgs

批处理文件

powershell -File Foobar.ps1 -ScriptArgs -versn="""1.0.0.0""",pattern="""FooBar.*"""

(3个引号-用于转义/显示1个引号)

(3 Quotes - For escaping/showing 1 quote)

但是无论我做什么 Write-Host $ ScriptArgs.Count 打印 1

But no matter what I do Write-Host $ScriptArgs.Count prints 1

我想要接收2个元素的变量

I want the variable to receive 2 elements

[0] 将为 -versn = 1.0 .0.0

[1] 将为 -pattern = FooBar。*

在批处理文件中,我甚至尝试过分别设置变量

In batch file, I even tried setting variables individually

set @sArgs = -versn="""1.0.0.0"""
set @sArgs = %sArgs%;-pattern="""FooBar.*"""
powershell -File Foobar.ps1 -ScriptArgs %sArgs%

但是控制台以某种方式运行,就像未创建变量一样并以

but the console somehow runs as if the variable is not created and run it as

powershell -File Foobar.ps1 -ScriptArgs

并引发错误


缺少参数'ScriptArgs'的参数。指定类型为 System.String [] 的参数,然后重试

什么我应该改变以实现这一目标吗?

What should I change to achieve this?

推荐答案

批处理文件中调用PowerShell脚本时(来自 cmd.exe 带有-文件 ,其中有直接的方式将值的数组传递给PowerShell array 参数:

When calling a PowerShell script from from a batch file (from cmd.exe) with -File, there is no way to directly pass an array of values to a PowerShell array parameter:


  • 如果指定不带空格的数组值,则将它们视为单个元素;例如:

  • If you specify the array values without spaces, they are considered a SINGLE element; e.g.:


  • 一个,两个,三个成为单个数组元素

  • one,two,three becomes a SINGLE array element

如果您确实使用空格,则尾随逗号将成为参数的一部分,并且仅第一个值绑定到数组参数

If you do use spaces, the trailing commas become part of the arguments, and only the FIRST value is bound to the array parameter:


  • 一,二,三-一个,-包括结尾的-成为 only 数组元素,两个,三个,被视为单独参数。

  • one, two, three - one, - including the trailing , - becomes the only array element, two, and three, are considered separate arguments.

解决方法是:


  • 任一使用 -Command 调用脚本-请参见底部。

  • Either: use -Command to invoke the script - see bottom section.

或者使用 -File ,以不同的方式声明参数并以不同的方式传递参数:

Or, with -File, declare the parameter differently and pass arguments differently:


  • 属性将数组参数修饰为
    [Parameter(ValueFromRema iningArguments)]

  • 将数组元素作为以空格分隔的单个参数没有参数名称

  • decorate the array parameter with attribute [Parameter(ValueFromRemainingArguments)]
  • and to pass the array elements as individual, space-separated arguments, without their parameter name.

在您的情况下:


  • 声明参数:

  • Declare the parameter:

[Parameter(ValueFromRemainingArguments)]
[string[]] $ScriptArgs


  • 使用分隔,空格分隔的参数而不使用参数名称调用脚本,以将它们作为数组绑定到 $ ScriptArgs

  • Invoke the script with separate, space-separate arguments not preceded by the parameter name to bind them as an array to $ScriptArgs:

    powershell -File Foobar.ps1 "-versn=\"1.0.0.0\"" "pattern=\"FooBar.*\""
    




    • 请注意以下引用:

      • Note the quoting:


        • 每个参数整体包含在 ... -并非绝对必要,但要防止使用-前缀的值,例如 -version 被误认为是p

        • Each argument as a whole is enclosed in "..." - this isn't strictly necessary, but guards against --prefixed values such as -version being mistaken for parameter names.

        字符。要保留为 literal 的字符是 \ 转义的(与`-相反转义,必须在 PowerShell中使用)。

        " chars. to be retained as literals are \-escaped (which contrasts with the `-escaping that must be used from within PowerShell).

        此方法的固有限制


        • 方法不支持多个数组参数,因为只有一个可以用 ValueFromRemainingArguments 。

        不能在位置上传递任何 other 自变量 (即,您必须在参数前面加上参数名称才能传递给其他参数)。

        You cannot pass any other arguments positionally (i.e., you must precede arguments to pass to other parameters with their parameter name).

        替代:使用 -Command 代替 -File

        Alternative: Using -Command instead of -File:

        powershell -command "./Foobar.ps1 '-versn=\"1.0.0.0\"', '-pattern=\"FooBar.*\"'"
        

        注意:


        • 传递给 -Command 的内容被视为一段PowerShell代码-无论是否传递了参数或多个参数-在后一种情况下,它们只是简单地连接带空格,然后 result

        • What you pass to -Command is treated as a piece of PowerShell code - whether you pass a single argument or multiple ones - in the latter case, they are simply concatenated with spaces and the result is then interpreted as PowerShell code.

        含义为:


        • 要在当前目录中执行脚本,必须使用 path .\ )引用该脚本才能执行该脚本

        • To execute a script in the current directory, you must refer to it with a path (.\) - in order to execute it.


        • 此外,如果脚本路径包含空格或其他shell元字符,则必须引用它并通过调用它。 & ,呼叫运算符;例如& '.\Foo Bar.ps1'

        • Additionally, if the script path contains whitespace or other shell metacharacters, you must quote it and invoke it via &, the call operator; e.g., & '.\Foo Bar.ps1'

        您可以照常传递数组参数, 除了,必须再次将 个字符转义为 \ ,而不是`

        You can pass array arguments as usual, except that " chars. must again be escaped as \", not `".

        还请注意,引数再次以整体引用 (太),在这种情况下,需要 来防止PowerShell解释 -versn -pattern 作为参数名称。

        Also note that again the arguments are quoted as a whole (too), which in this is case required to prevent PowerShell from interpreting -versn and -pattern as parameter names.


        • 鉴于整个命令必须在 ... <之内传递/ code>,仍然可以使用 进行嵌入式报价,这很尴尬,因为您必须 combine \ -和`的转义;例如, \ -pattern =`\ FooBar。*`\ \

        • Given that the overall command must be passed inside "...", performing the embedded quoting with ", which still possible, is quite awkward, because you then have to combine \- and `-escaping; e.g., \"-pattern=`\"FooBar.*`\"\"

        通常,请注意所有常用的PowerShell解析规则都适用,例如,使用 literal $ ,则必须将其转为`$

        Generally, be aware that all usual PowerShell parsing rules apply, so that, for instance, to use a literal $, you must escape it as `$.

        这篇关于将string []从批处理文件(包含双引号“)传递到powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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