Nilpo`s Scripting Secrets, Vol I
(Page 1 of 4 )
As I was going through my own personal script repository, I noticed that I had compiled quite a number of simple scripts and code snippets that I had either found or created to perform simple but often necessary scripting tasks. A good many of them are workarounds developed to add functionality that is otherwise unavailable in WSH or VBScript alone. I decided to package them up and offer them to my readers.
Some of these are simple and may not appear to have any immediate use. Others, however, you will find to be quite priceless. They are presented here in no particular order. If you don’t have a use for the code in particular, try to at least learn from them by seeing how their techniques were used to accomplish each task.
Forcing a specific scripting engine
As you well know, the Windows Script Host provides two different scripting engines: cscript.exe and wscript.exe. While most scripts can be run equally well in one or the other, there are often times when one or the other is preferred or even necessary. Since the default engine can be changed from system to system, it is nice to include some code in your script to force the use of a necessary engine for compatibility purposes.
If LCase(Right(WScript.FullName, 11)) <> "cscript.exe" Then
strPath = WScript.ScriptFullName
strCommand = "%comspec% /k cscript " & Chr(34) & strPath & chr(34)
CreateObject("WScript.Shell").Run(strCommand)
WScript.Quit
End If
This simple If statement can be placed at the top of your script to force it to run using Cscript.exe. It uses the WScript object to determine the current scripting engine by examining the FullName property.
If the script is running in the correct engine, the If statements exits and script execution continues. Otherwise, it launches the script again in the correct engine from the command line and then exits the first instance.
Next: Determine if a user is an Administrator >>
More Windows Scripting Articles
More By Nilpo