Can you call a WSH script from within an existing VBS WSH logon script?
Hello, Cheryl. Quite simply, yes you can. There are a couple of methods for doing this. I’ll show you the easiest two first and then I’ll document a third, more robust way, in a future article.
To do this, you can rely on the WScript Object Model. It provides us with two methods designed specifically for calling external scripts or applications.
The first is the Run method made available by the WshShell object. It requires a command line and accepts the following syntax:
object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Object is a required reference to the WshShell object. The Run method accepts three parameters.
The first is a required command line string. This must be a valid Windows command line.
The second is an optional integer value that represents the program’s window style. Not all programs will use this. Acceptable values are listed in the table below.
|0||Hides the window and activates another window.|
|1||Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.|
|2||Activates the window and displays it as a minimized window.|
|3||Activates the window and displays it as a maximized window.|
|4||Displays a window in its most recent size and position. The active window remains active.|
|5||Activates the window and displays it in its current size and position.|
|6||Minimizes the specified window and activates the next top-level window in the Z order.|
|7||Displays the window as a minimized window. The active window remains active.|
|8||Displays the window in its current state. The active window remains active.|
|9||Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.|
|10||Sets the show-state based on the state of the program that started the application.|
The third is a boolean value the indicates whether or not you script should wait for execution to complete before continuing to the next statement. If omitted, this defaults to false.
Keeping this in mind, you can easily launch one script from within another by using the command line. You’ll need to decide between using cscript.exe and wscript.exe and you’ll need the full path to the script you wish to launch.
Set WshShell = CreateObject("WScript.Shell") strScriptPath = "C:\temp\myOtherScript.vbs" WshShell.Run "cscript.exe " & Chr(34) & strScriptPath & Chr(34), 0, vbTrue
Notice how I’ve used Chr(34) on either side of my path. This returns a quotation mark ensuring that I don’t run into any problems if my script path contains any spaces. The actual command line being used looks like this:
You can also add any command line switches that you require. Any valid command line will work. This is essentially the same as executing a command line from the Run dialog box on your start menu.
Hope this answers your question!
On a side note, the WshShell object also has a Exec method for executing command lines. It has less flexibility but will allow you to interact with the newly launched script when used correctly.