20 August 2019

Batch file wait for process using PowerShell integration

Recently, I wanted a simple batch file to run the ccmsetup.exe file. I could have used PowerShell, but for debugging the parameters, I wanted it quick and easy to modify. One of the issues I had was getting the batch script to wait for the ccmexec.exe to install before proceeding. I saw some batch solutions that seemed to be kind of cumbersome. I decided to write a PowerShell one-liner that would continuously loop until the desired process ended. To do this, I used the following one-liner and pasted it into the batch file after the ccmsetup.exe was triggered. I wrote this so that all you have to do is change the assigned value to the $ProcessName. I included in the script the ability to remove the file extension in the event it is accidentally used. You can see below how I used it in the batch script:




 powershell.exe -executionpolicy bypass -command "&{$ProcessName='ccmsetup';Do {$Process=get-process ($ProcessName.split('.')[0]) -erroraction SilentlyContinue;Start-Sleep -seconds 2} While ($Process -ne $null)}"  

0 comments:

Post a Comment