Powershell call batch within scriptblock

十年热恋 提交于 2019-12-25 03:32:28

问题


I have the script below which is not working the way I want. Initially, I would like to pass install.cmd to function which will use the "Start-Job" in the background so that it doesn't freeze up the main Powershell window. But I can't get it to call the 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
}

回答1:


I believe you overkilled it (a bit). This works:

$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 to the rescue: https://stackoverflow.com/a/25020293/4593649

Also, this variation works fine:

$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
}

Tested with PShell ver4. Cheers!



来源:https://stackoverflow.com/questions/29569523/powershell-call-batch-within-scriptblock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!