天道酬勤,学无止境

Job

Powershell如何在Start-Job的Scriptblock里传参?

如何在Start-Job的Scriptblock里传参?方法1:利用本地变量,从一个可扩展的字符串,使用[scriptblock]::create方法创建脚本块:$v1 = "123" $v2 = "asdf" $sb = [scriptblock]::Create("Write-Host 'Values are: $v1, $v2'") $job = Start-Job -ScriptBlock $sb # 另一种写法 [scriptblock]$sb = { Write-Host "Values are: $v1, $v2" }方法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

2021-03-26 21:54:02    分类:博客    powershell   start   Job    powershell