首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell -将扩展参数传递到Start-Job

PowerShell -将扩展参数传递到Start-Job
EN

Stack Overflow用户
提问于 2014-07-29 15:37:11
回答 2查看 1.4K关注 0票数 3

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

代码语言:javascript
复制
$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"

它失败了

代码语言:javascript
复制
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设置正确。

有什么想法吗?

提前谢谢你!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-29 16:26:29

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

代码语言:javascript
复制
$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"

它允许您在本地运行脚本时使用相同的参数散列。

这段代码:

代码语言:javascript
复制
$(&{$args}@arguments)

嵌入在可展开字符串中的参数将为参数创建参数: Value对:

代码语言:javascript
复制
$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
票数 5
EN

Stack Overflow用户

发布于 2014-07-29 15:43:43

单引号是文字字符串符号,您将"-Name“参数设置为字符串$config.Name而不是Value of $config.Name。若要使用该值,请使用以下内容:

代码语言:javascript
复制
$arguments= @()
$arguments+= ("-Name", $config.Name)
$arguments+= ("-Account", $config.Account)
$arguments+= ("-Location", $config.Location)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25019314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档