PowerShell的 - 传递扩展参数启动作业cmdlet的 [英] PowerShell - Passing expanded arguments to Start-Job cmdlet

查看:156
本文介绍了PowerShell的 - 传递扩展参数启动作业cmdlet的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正试图创建一个变量数组,然后作为扩大到一个脚本,应当由启动工作运行通过此阵。但实际上它失败,我们无法找到原因。也许有人可以帮助!?

We're trying to create an array with variables and then pass this array as expanded to a script, which shall be run by Start-Job. But actually it fails and we are unable to find the reason. Maybe someone can help!?

$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设置是否正确。

Even though $config.name is set correctly.

任何想法?

感谢你在前进!

推荐答案

我用这个方法传递命名参数:

I use this method for passing named parameters:

$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"

它可以让你使用相同的参数哈希你会使用到图示的脚本,如果你在本地运行它。

It lets you use the same parameter hash you'd use to splat to the script if you were running it locally.

code的该位:

$(&{$args}@arguments)

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

embedded in the expandable string will create the Parameter: Value pairs for the 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

这篇关于PowerShell的 - 传递扩展参数启动作业cmdlet的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆