Windows Script Host: CScript.exe and WScript.exe Options

davegate Posts: 182 Points: 894
Posted: Wednesday, December 23, 2015 7:12:17 PM

To determine which version of cscript.exe you are running, type cscript at the command prompt.

C:\>cscript
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Usage: CScript scriptname.extension [option...] [arguments...]

codeling Posts: 1103 Points: 4638
Posted: Thursday, June 14, 2018 11:26:57 AM

To check default host:

c:\>ftype VBSFile

VBSFile="%SystemRoot%\System32\WScript.exe" "%1" %*

Or check

HKLM\Software\ClassesVBSFile\Shell\Open\Command

 

 


codeling Posts: 1103 Points: 4638
Posted: Thursday, June 14, 2018 11:36:47 AM

On 64-bit Windows, we have two different sets of Window Scripting Host (WSH), 32-bit or 64-bit. The defaults are located at C:\Windows\System32, where cscript.exe or wscript.exe (with GUI, e.g. message prompt) is used run the *.vbs or *.js WSH files, and these are 64-bit

When you want to deploy some scripts that require a 32-bit COM (Component Object Model) object, you don’t want the system end up showing this error dialog. What we can do here is to check whether which script engine is used at the begining of the script execution. If it is 32-bit, then carry on otherwise, show a information dialog, like this.

com-ok Make Sure Run Script under 32-bit cscript.exe or wscript.exe when 32-bit COM object is required. exception handling implementation javascript programming languages tricks vbscript windows windows scripting host

We can check WScript.FullName to see its scripting engine file path contains the word ‘syswow64’, this works for most cases, unless some one intentionally puts a 64-bit engine under this folder. WoW64 stands for Windows 32-bit on Windows 64-bit. If a engine is 32-bit, then it surely be able to create 32-bit COM object.

The following VBScript checks the engine file path and exit printing the above dialog if it seems to be a 64-bit engine, e.g. ‘C:\windows\system32\cscript.exe’.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Dim Mode
If Not IsEmpty(WScript) Then
    If (InStr(LCase(WScript.FullName), "syswow64")) Then    
        'WScript.Echo "OK, It is in 32-bit"
        Mode = 0
    Else
        WScript.Echo "Please Use SysWOW64\cscript.exe or wscript.exe (32-bit) to lauch this script."    
        WScript.Quit
    End If
Else
    'We don't have access to WScript, most-likely-we are using MS Script Control.
    Mode = 1                       
End If
 
' Set the corresponding Message Box prompt.
Sub MessageBox(str)
    If Mode = 0 Then
        WScript.Echo str
    Else
        Msgbox str
    End If
End Sub
Dim Mode
If Not IsEmpty(WScript) Then
	If (InStr(LCase(WScript.FullName), "syswow64")) Then	
		'WScript.Echo "OK, It is in 32-bit"
		Mode = 0
	Else
		WScript.Echo "Please Use SysWOW64\cscript.exe or wscript.exe (32-bit) to lauch this script."	
		WScript.Quit
	End If
Else
	'We don't have access to WScript, most-likely-we are using MS Script Control.
	Mode = 1                       
End If

' Set the corresponding Message Box prompt.
Sub MessageBox(str)
	If Mode = 0 Then
		WScript.Echo str
	Else
		Msgbox str
	End If
End Sub

The above script also sets the correct message prompt function. If WScript is not available (empty), it is likely that we are using MS Script Control, where WScript.Echo is not available, we must use the alternative, Msgbox.

However, translating the above into JScript is a bit lengthy. Let’s look at the code first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(function () {
    var mode;
    try {
        if (WScript != undefined) {
            var path = WScript.FullName.toLowerCase();
            if (path.indexOf('syswow64') >= 0) {
                mode = 0;
            } else {
                WScript.Echo("Please Use SysWOW64\\cscript.exe or wscript.exe (32-bit) to lauch this script.");
                WScript.Quit();
            }
        } else {
            mode = 1;
        }
    } catch (err) {
        mode = 1;
    }
    var obj = new ActiveXObject("WScript.Shell");
    function MessageBox(str) {
        if (mode == 0) {
            WScript.Echo(str);
        } else {
            obj.popup(str);
        }
    }
})();
(function () {
	var mode;
	try {
		if (WScript != undefined) {
			var path = WScript.FullName.toLowerCase();
			if (path.indexOf('syswow64') >= 0) {
				mode = 0;
			} else {
				WScript.Echo("Please Use SysWOW64\\cscript.exe or wscript.exe (32-bit) to lauch this script.");
				WScript.Quit();
			}
		} else {
			mode = 1;
		}
	} catch (err) {
		mode = 1;
	}
	var obj = new ActiveXObject("WScript.Shell");
	function MessageBox(str) {
		if (mode == 0) {
			WScript.Echo(str);
		} else {
			obj.popup(str);
		}
	}
})();

We have to wrap the check in trycatch because if not, MS JScript Engine will complain that WScript is not defined under MS Script Control. As JScript does not provide inbuilt Msgbox as VBScript does, it can create COM object WScript.Shell to use its popup function to prompt a message, which can also be used in VBScript.

Users browsing this topic
Guest