PowerShell - 開始jobコマンドレットに拡張された引数を渡す
-
21-12-2019 - |
質問
変数を持つ配列を作成してから、この配列をスクリプトに拡張して渡し、これはスタートジョブによって実行されます。しかし実際には失敗し、その理由を見つけることができません。多分誰かが助けることができる!?
$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
. 他のヒント
シングルクォートはリテラル文字列シンボルです。 "-name"引数を文字列$config.Name
ではNot Value of $config.Name
に設定しています。値を使用するには、次のようにします。
$arguments= @()
$arguments+= ("-Name", $config.Name)
$arguments+= ("-Account", $config.Account)
$arguments+= ("-Location", $config.Location)
. 所属していません StackOverflow