2

We're launching a PowerShell script with the help of vbs to avoid the pop-up it generates. This works flawlessly as you can see below:

PS_Launcher.vbs:

'run window totally hidden
Dim oSHELL
Set oSHELL = CreateObject("WScript.Shell")
oSHELL.CurrentDirectory = "C:\users\bob\AppData\Local\Temp"
oSHELL.Run "powershell.exe -ExecutionPolicy Bypass -NoLogo -File .\PS_Script.ps1", 0
Set oSHELL = Nothing

The only problem is when we run this from within a scheduled task or from the command prompt (CMD), it doesn't wait for the PowerShell script to finish and immediately returns the prompt.

Is there a way to have it wait for the PowerShell script to finish before continuing?

We've tried the following as described in bWaitForReturn, but no luck:

oSHELL.Run "powershell.exe -ExecutionPolicy Bypass -NoLogo -File .\PS_Script.ps1", 0, True

PS_Script.ps1:

Start-Sleep -Seconds 15

Get-WmiObject -Class Win32_MappedLogicalDisk |
    Select-Object Name, ProviderName |
    Export-Csv .\PS_Data.txt -NoTypeInformation
1
  • It's one 100% sure VBScript, as the PowerShell script only contains Start-Sleep -Seconds 15 Commented Jan 26, 2017 at 13:39

1 Answer 1

5

Your 2nd attempt where you included the true argument in Run should work.

The fact that the prompt returns immediately after running the script from command prompt doesn't mean that the script is not waiting, as the script runs in a different environment to cmd.

Take this example:

PowerShell

Start-Sleep -Seconds 5

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[System.Windows.Forms.MessageBox]::Show("powershell END") 

VBScript

Dim oSHELL
Set oSHELL = CreateObject("WScript.Shell")
oSHELL.Run "powershell.exe -ExecutionPolicy Bypass -NoLogo -File C:\test\test.ps1", 0, True
Set oSHELL = Nothing
MsgBox "vbs END"

Now whether I run the VBScript by double-clicking it or calling test.vbs from the command prompt, I see the expected results, i.e.:

  1. Wait 5 seconds
  2. Display powershell END message
  3. Display vbs END message once the powershell message has been dismissed

However, when running it from cmd, the prompt returns to me as soon as I call test.vbs, even though the above steps are still completed correctly.

Perhaps you could try adding some message boxes like this in your scripts to help you track what is going on during testing.

Sign up to request clarification or add additional context in comments.

8 Comments

We're starting the vbscript from the Task Scheduler. Found something here that might help with keeping the task in status Running. Thx for the tips though!
@DarkLite1 The same applies. When I call my above vbs from a scheduled task it stays in Running status for 5 seconds - displays the powershell prompt, and then displays the vbs prompt. Which version of windows are you running, and did you try including message boxes in your scripts?
Make sure to run VBScripts with cscript.exe when you run them as scheduled tasks, though.
@AnsgarWiechers I noticed some discussion on that on a few forums, but my scripts seem to run the sme without calling through cscript - Do you know the implications of not including it? (I'm using Windows 10)
@Bassie With cscript.exe regular output (error messages, WScript.Echo, ...) goes to STDOUT/STDERR instead of creating a popup message that blocks further execution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.