【发布时间】:2017-05-07 12:41:20
【问题描述】:
我有一个使用多个参数的脚本,其中一些包含空格。该脚本是从另一个脚本调用的,因此我将参数从带有调用脚本的变量传递给它。
调用脚本:
$script = "C:\Path\script.ps1"
$arg1 = "SomeValue"
$arg2 = "1234"
$arg3 = @("Value1","Some Value","Value 2")
$arg4 = $true
Invoke-Command $script -Arg1 $arg1 -Arg2 $arg2 -Arg3 $arg3 -Arg4 $arg4
调用的脚本如下所示:
param (
[Parameter(Mandatory=$false,Position=0)]
[String]$arg1,
[Parameter(Mandatory=$false,Position=1)]
[String]$arg2,
[Parameter(Mandatory=$false,Position=2)]
[array]$arg3,
[Parameter(Mandatory=$false,Position=3)]
[bool]$arg4
)
# Do stuff with the arguments
当我调用脚本时,出现以下错误:
"A positional parameter cannot be found that accepts argument 'Some'."
我还在 powershell 窗口中手动调用了脚本(绕过调用脚本),如下所示:
powershell.exe -ExecutionPolicy bypass C:\Path\script.ps1 -Arg1 "SomeValue" -Arg2 "1234" -Arg3 @("Value1","Some Value","Value 2") -Arg4 $true
powershell.exe -ExecutionPolicy bypass C:\Path\script.ps1 -Arg1 "SomeValue" -Arg2 "1234" -Arg3 "Value1","Some Value","Value 2" -Arg4 $true
powershell.exe -ExecutionPolicy bypass C:\Path\script.ps1 -Arg1 "SomeValue" -Arg2 "1234" -Arg3 "Value1","SomeValue","Value2" -Arg4 $true
powershell.exe -ExecutionPolicy bypass C:\Path\script.ps1 -Arg1 "SomeValue" -Arg2 "1234" -Arg3 "Value1,SomeValue,Value2" -Arg4 $true
这些变体都不起作用。我还通过将 Arg3 值更改为 (,$args) 来尝试找到 here 的想法,但这不起作用。我还更改了 here 找到的参数类型,但这也没有用。
目标是能够通过参数/参数将多个变量(一些带有空格)传递给脚本。
EDIT 12/22/16:目标包括从快捷方式/键入命令传递相同的信息。例如,我的调用脚本在注册表中创建了一个 RunOnce 条目来引用被调用的脚本,并将参数放入调用中,就像上面的手动示例一样。它们都不起作用。
Set-ItemProperty $RegROPath "(Default)" -Value "powershell.exe -ExecutionPolicy Bypass $scriptPath $argumentList" -type String
【问题讨论】:
-
powershell.exe -ExecutionPolicy bypass --% "C:\Path\script.ps1 -Arg1 \"SomeValue\" -Arg2 \"1234\" -Arg3 \"Value1\",\"Some Value\",\"Value 2\" -Arg4 $true" -
您好 PetSerAl,我将以编程方式构建它,因此值需要来自 splatted 数组。
-
可以的。您只需要在任何地方应用适当的转义即可。
-
所以我需要通过转义在数组元素中包含引号?我不明白。
标签: powershell