gpt4 book ai didi

arrays - PowerShell - 将扩展参数传递给 Start-Job cmdlet

转载 作者:行者123 更新时间:2023-12-02 23:13:57 25 4
gpt4 key购买 nike

我们正在尝试创建一个包含变量的数组,然后将该数组作为扩展传递给一个脚本,该脚本将由 Start-Job 运行。但实际上它失败了,我们无法找到原因。也许有人可以帮忙!?

$arguments= @()
$arguments+= ("-Name", '$config.Name')
$arguments+= ("-Account", '$config.Account')
$arguments+= ("-Location", '$config.Location')

#do some nasty things with $config

Start-Job -ScriptBlock ([scriptblock]::create("& .'$ScriptPath' [string]$arguments")) -Name "Test"

它失败了

Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
+ CategoryInfo : InvalidData: (:) [Select-AzureSubscription], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.WindowsAzure.Commands.Profile.SelectAzureSubscriptionCommand
+ PSComputerName : localhost

即使 $config.name 设置正确。

有什么想法吗?

提前致谢!

最佳答案

我使用此方法传递命名参数:

$arguments = 
@{
Name = $config.Name
Account = $config.Account
Location = $config.Location
}

#do some nasty things with $config

Start-Job -ScriptBlock ([scriptblock]::create("&'$ScriptPath' $(&{$args}@arguments)")) -Name "Test"

如果您在本地运行它,它允许您使用与 splat 脚本相同的参数哈希。

这段代码:

$(&{$args}@arguments)

嵌入可扩展字符串将创建参数:参数的值对:

$config = @{Name='configName';Account='confgAccount';Location='configLocation'}
$arguments =
@{
Name = $config.Name
Account = $config.Account
Location = $config.Location
}

"$(&{$args}@arguments)"

-Account: confgAccount -Name: configName -Location: configLocation

关于arrays - PowerShell - 将扩展参数传递给 Start-Job cmdlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25019314/

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