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 -ArgumentList $v1,$v2

  复制代码

  (编辑:雷林鹏 来源:网络)

posted @ 2018-07-19 10:20  雷林鹏  阅读(1460)  评论(0编辑  收藏  举报