我们正在尝试创建一个带有变量的数组,然后将这个数组作为扩展传递到脚本中,脚本将由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设置正确。
有什么想法吗?
提前谢谢你!
发布于 2014-07-29 16:26:29
我使用此方法传递命名参数:
$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"它允许您在本地运行脚本时使用相同的参数散列。
这段代码:
$(&{$args}@arguments)嵌入在可展开字符串中的参数将为参数创建参数: Value对:
$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发布于 2014-07-29 15:43:43
单引号是文字字符串符号,您将"-Name“参数设置为字符串$config.Name而不是Value of $config.Name。若要使用该值,请使用以下内容:
$arguments= @()
$arguments+= ("-Name", $config.Name)
$arguments+= ("-Account", $config.Account)
$arguments+= ("-Location", $config.Location)https://stackoverflow.com/questions/25019314
复制相似问题