将参数从 powershell 脚本传递给另一个

passing parameters from powershell script to another

我调查过这个论坛。但似乎不太了解它,无法找到我的答案。我是 Powershell 的新手。

我遇到的情况是我已经启动了一个 ps1-脚本,需要执行另一个 ps1-脚本以在后台启动,并将参数从第一个脚本传递到第二个.

像这样 (script1.ps1):

$var1 = "p1 (string value)"
$var2 = "p2 (string value)"
$var3 = "p3 (string value)"
$var4 = 4 # numeric value

$counter = 1

while ($counter -le $var4)
{
    #call to script2.ps1 with passing the $var1 through $var4 as
    #parameters so that script2.ps1 executes as a background process
    #and works with the received parameters $var1 through $var4
    # ??
    $counter= $counter + 1
}

exit
########################################

我找不到 1 种方法来传递参数并在后台启动 script2。ps1。

真的希望有人能帮助我。

THIA, 维姆

这对你有帮助About_Jobs

获取帮助Start-Job

https://sqlblog.org/2011/01/29/powershell-start-job-scriptblock-sad-panda-face

这对我有用。

$var1 = "p1 (string value)"
$var2 = "p2 (string value)"
$var3 = "p3 (string value)"
$var4 = 4 # numeric value

$counter = 1
while ($counter -le $var4)
{

    $Params = "-Param1 $var1 -Param2 $var2 -Param3 $var3"
    $Script = [scriptblock]::create("d:\temp\scriptname.ps1 $Params")
    Start-Job -ScriptBlock $Script

    $counter = $counter+1
}

exit