0

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
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Besiktas
  • 331
  • 1
  • 6
  • 23
  • You define `$Install = "C:\test\$App\Install.cmd"` but never reference it outside of that. I think you need your `Invoke-Command` to be `{Invoke-Command "cmd.exe /c $install"}` You're going to run into issues though because (assuming more than one application uses the Windows Installer) the Windows Installer will be busy with the first application installation when you try to start the second (and third etc) and the applications past the first are going to fail. – TheMadTechnician Apr 10 '15 at 20:06
  • ahh I actually forgot to add $install at the end of "-argumentlist" please see updated code above. I tried your code but it just gives me errors of "cmd was called from the above path..." Thanks as always Madtechnician. – Besiktas Apr 10 '15 at 20:17
  • Your scriptblock `{Invoke-Command (cmd / c)}` has no defined parameters to which you would pass that argument. What exactly do you expect it to do with the argumentlist? Should it just intrinsically know to tack it on to the end of the command being passed to `Invoke-Command`? – TheMadTechnician Apr 10 '15 at 21:05

1 Answers1

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!

Community
  • 1
  • 1
  • Hi Marin. Thanks for the answer, I found this script on another site (Sapien) and I'm trying to add it so that i calls the batches I have. Here is the link to the site. Would you be able to help me with implementing this? [link](http://www.sapien.com/blog/2012/05/16/powershell-studio-creating-responsive-forms/) – Besiktas Apr 14 '15 at 14:01
  • @Besiktas - what is it that you want to do, exactly? Have you tested the provided code? Does it work? I believe you should mark this question as answered and ask another question. But this time, please, describe what you want your script to do. I can see several scenarios: 1. running batch scripts on local machine and after all batch scripts have been started, waiting for them to stop, collecting data and presenting results; 2. running batch scripts on multiple remote machines using PSRemoting (which implies PSRemoting is enabled on target machines); 3. other ways of remote management, etc. – Marin Bakarić Smilevski Apr 14 '15 at 17:37