脚本块中的 Powershell 调用批处理

Powershell call batch within scriptblock

我的下面的脚本无法按我想要的方式运行。最初,我想将 install.cmd 传递给将在后台使用 "Start-Job" 的函数,这样它就不会冻结主 Powershell window。但我无法让它调用 install.cmd。

$Appname = @("Adobe_FlashPlayer", "Acrobat_Reader", "Microsoft_RDP")

function BatchJob{

    Param (
        [ScriptBlock]$batchScript,
        $ArgumentList = $null)
    #Start the batch
    $batch = Start-Job -ScriptBlock $batchScript -ArgumentList $ArgumentList

}

Foreach($App in $Appname){
    $Install = "C:\test$App\Install.cmd"
    Batchjob -batchscript {Invoke-Command (cmd / c)} -ArgumentList $install     
    Wait-Job $job
    Receive-Job $job
}

我相信你(有点)矫枉过正了。 这有效:

$Appname = @("Adobe_FlashPlayer", "Acrobat_Reader")

Foreach($App in $Appname){
    $Install = "C:\test$App\Install.cmd"
    $job = Start-Job ([scriptblock]::create("cmd /C $Install"))
    Wait-Job $job
    Receive-Job $job
}

*mjolinor 来救援:

另外,这个变体工作正常:

$Appname = @("Adobe_FlashPlayer", "Acrobat_Reader")

Foreach($App in $Appname){
    $Install = "C:\test$App\Install.cmd"
    $scriptBlock = ([scriptblock]::create("cmd /C $Install"))
    $job = Start-Job $scriptBlock
    Wait-Job $job
    Receive-Job $job
}

已使用 PShell ver4 进行测试。 干杯!