0

我有下面的脚本没有按我想要的方式工作。最初,我想将 install.cmd 传递给将在后台使用“Start-Job”的函数,这样它就不会冻结主 Powershell 窗口。但我无法让它调用 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
}
4

1 回答 1

1

我相信你过度杀戮(有点)。这有效:

$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 救援:https ://stackoverflow.com/a/25020293/4593649

此外,这种变体效果很好:

$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 测试。干杯!

于 2015-04-12T01:55:29.517 回答