Link to home
Start Free TrialLog in
Avatar of jfgray
jfgray

asked on

Wait for exit

I have the folling script that runs a batch file on a remote computer

$command = "c:\hello.bat"
$process = [WMICLASS]"\\$MachineName\ROOT\CIMV2:win32_process"  
$result = $process.Create($command)

How can I pause or wait to continue until this has completed?
ASKER CERTIFIED SOLUTION
Avatar of x-men
x-men
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jfgray
jfgray

ASKER

This seems to work but returns

Cannot find a process with the name "cmd". Verify the process name and call the cmdlet again.
At :line:50 char:41
+ do {Start-Sleep -s 1} While ((Get-Process <<<<  -Name cmd -computername $MachineName | Measure-Object).count -gt 0)


Is this expected ?
yes, if after the process ends, the get-process will error because it is no longer running.

To suppress the error message, change the $ErrorActionPreference to “silentlycontinue”

$ErrorActionPreference = “silentlycontinue”


Avatar of jfgray

ASKER

THANK YOU ! ! !