如何在Start-Job的Scriptblock里传参?
方法1:
利用本地变量,从一个可扩展的字符串,使用[scriptblock]::create方法创建脚本块:
$v1 = "123" $v2 = "asdf" $sb = [scriptblock]::Create("Write-Host 'Values are: $v1, $v2'") $job = Start-Job -ScriptBlock $sb
方法2:
在InitializationScript中设置变量
$Init_Script = { $v1 = "123" $v2 = "asdf" } $sb = { Write-Host "Values are: $v1, $v2" } $job = Start-Job -InitializationScript $Init_Script -ScriptBlock $sb
方法3:
使用-Argumentlist参数
$v1 = "123" $v2 = "asdf" $sb = { Write-Host "Values are: $($args[0]), $($args[1])" } $job = Start-Job -ScriptBlock $sb -ArgumentList $v1,$v2
原文地址:http://blog.51cto.com/ultrasql/2068010
时间: 2024-10-12 08:39:52