Powershell如何在Start

作者:admin发表于:2019-12-06

办法1:

运用本地变量,从一个可扩展的字符串,运用[scriptblock]::create办法创立脚本块:

$v1 = "123"

$v2 = "asdf"

$sb = [scriptblock]::Create

$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: $, $"

}

$job = Start-Job -ScriptBlock $sb -ArgumentList $v1,$v2

仿制代码