gpt4 book ai didi

powershell - Powershell:将多个值(带空格)传递给参数

转载 作者:行者123 更新时间:2023-12-02 23:41:27 24 4
gpt4 key购买 nike

我有一个使用多个参数的脚本,其中一些包含空格。该脚本是从另一个脚本调用的,因此我使用调用脚本从变量中将参数传递给它。

调用脚本:

$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,但这也不起作用。

目标是能够通过参数/参数将多个变量(有些带有空格)传递给脚本。

编辑12/22/16 :目标包括从快捷方式/键入的命令传递相同的信息。例如,我的调用脚本在注册表中创建一个RunOnce条目以引用被调用的脚本,并将参数放在调用中,就像上面的手动示例一样。它们都不起作用。
Set-ItemProperty $RegROPath "(Default)" -Value "powershell.exe -ExecutionPolicy Bypass $scriptPath $argumentList" -type String

最佳答案

Invoke-Command&替换.
如果要输出所有内容,请使用&;如果要在当前上下文中运行,请使用.(例如,保留所有设置的变量)
Get-Help about_Scripts以获得更多详细信息(或阅读online version here)

编辑:忘记提及了,不是您的脚本引发该错误,而是Invoke-Command。如果绝对必须使用Invoke-Command,则需要(例如,远程运行)将参数作为参数ArgumentList传递,如下所示:

$script = "C:\Path\script.ps1"
$argumentList = @(
'-arg1 "SomeValue"',
'-arg2 1234',
'-arg3 @("Value1","Some Value","Value 2")',
'-arg4 $true'
)
Invoke-Command -FilePath $script -ArgumentList $argumentList

编辑2:

I will try your suggestion as soon as I am able to. One question, what if I need to add a conditional argument? Currently, I add arguments to the list with $argumentlist += ("arg5", "value"). Some of them are conditional: if ($bool) {$argumentlist += ("arg5", "value")}. Is there a way to do that in your example?



是的,可以,示例中的 $argumentList变量是一个数组,与其他数组一样。可以一次全部定义,也可以将其定义为空并添加到以后,或任意混合。


$argumentList = @(
'-arg1 "SomeValue"',
'-arg2 1234',
'-arg3 @("Value1","Some Value","Value 2")',
'-arg4 $true'
)
if ($bool) {
$argumentList += '-arg5 "value"'
}
Invoke-Command -FilePath $script -ArgumentList $argumentList

但是同样,除非您在远程计算机或PSSession上运行该命令,否则应使用 &或点源( .)。您仍然可以使用splatting( about_Splatting)有条件地添加参数


$scriptParamsSplat = @{
arg1 = "SomeValue"
arg2 = 1234
arg3 = @("Value1","Some Value","Value 2")
arg4 = $true
}
if ($bool) {
$scriptParamsSplat.arg5 = "value"
}
& 'C:\Path\To\script.ps1' @scriptParamsSplat

关于powershell - Powershell:将多个值(带空格)传递给参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41273818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com