2

変数を含む配列を作成し、この配列を展開してスクリプトに渡し、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 が正しく設定されていても。

何か案は?

前もって感謝します!

4

2 に答える 2

3

名前付きパラメーターを渡すためにこのメソッドを使用します。

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

展開可能な文字列に埋め込まれていると、引数の Parameter: 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-29T16:26:29.563 に答える