0

I have a PowerShell script that I need to run from a VBScript and I need the VBScript to wait on the PowerShell to finish (but the two don't need to communicate at all). Here's code that will execute the powershell, but the VBScript doesn't wait:

Set objShell = (CreateObject("Wscript.shell"))

objShell.Run ("powershell -noexit -file e:\powershell\vb_to_powershell_test.ps1")

MsgBox("Powershell execution complete")

This script will pop up with "Powershell execution complete" instantly, while the PowerShell is still running.

Apparently there is a True flag I could add that forces VB to wait. Like so, maybe:

objShell.Run ("powershell -noexit -file e:\powershell\vb_to_powershell_test.ps1", True)

That isn't working for me, as the script always complains about "cannot use parenthesis when calling a sub", which is a whole thing.

I suspect this is a VisualBasic vs. VB.NET issue, but I'm stuck with VisualBasic here.

What am I doing wrong?

Community
  • 1
  • 1
crowhill
  • 2,398
  • 3
  • 26
  • 55
  • This other post [may be what you are looking for](http://stackoverflow.com/questions/10279404/vbscript-how-to-make-program-wait-until-process-has-finished) – BenH Sep 19 '16 at 17:48

1 Answers1

3

Synchronous/asynchronous execution is controlled by the 3rd parameter of the Run method (2nd parameter controls the window style). Set that parameter to True to run the command synchronously:

objShell.Run "powershell -noexit -file e:\some.ps1", 1, True

The second parameter can be omitted, but you still need to maintain the position:

objShell.Run "powershell -noexit -file e:\some.ps1", , True

Parentheses can only be used if you call the method with a Call statement:

Call objShell.Run("powershell -noexit -file e:\some.ps1", 1, True)

or in situations where you use the return value, e.g. when assigning it to a variable:

retval = objShell.Run("powershell -noexit -file e:\some.ps1", 1, True)

See here for further explanation.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328