How to determine the code for the default script host on your computer before you run the script _vbs

Source: Internet
Author: User
Tags constant
Ask:
Hello, Scripting Guy! I know that when you run a script, you can use code to determine whether the script runs in WScript or CScript. But what I don't understand is: how do you determine the default script host on your computer before you run the script?
--at
For:
Hello, at. You know, it's not uncommon for people to be completely stumped by the Scripting Guys, but we have to admit that this is a challenging question indeed. (Well, well: well, maybe it's not enough to get the Scripting Guys out of it; in fact, it's quite simple.) But you don't need to know that. We do not know how to answer this question, but we know that in order to help you solve this problem, we will need a lot of hard work and dedication.
But in the face of difficulties, did the Scripting Guys give up? Well, actually, we've given up. However, when dealing with irrelevant things, we found the answer by accident. The following script, which uses a somewhat circuitous method, tells you the default script host on your computer:
Copy Code code as follows:

Const HKEY_CLASSES_ROOT = &h80000000
StrComputer = "."
Set objregistry = GetObject ("winmgmts:\\" & StrComputer & "\root\default:StdRegProv")
strKeyPath = "Vbsfile\shell\open\command"
Objregistry.getexpandedstringvalue Hkey_classes_root,strkeypath,vbnullstring,strvalue
strvalue = LCase (strvalue)
If InStr (strvalue, "Wscript.exe") Then
WScript.Echo "Wscript"
Else
WScript.Echo "CScript"
End If

As you can see, information about the default script host is stored in the registry, although not in a particularly obvious location. All you need to do is open HKEY_CLASSES_ROOT, locate the registry key Vbsfile\shell\open\command, and then view the default values. (The default is an exception to an unnamed registry value that appears in Regedit as "(default).") The value of (default) will be the path of the script Host executable program (for example,%systemroot%\system32\wscript.exe). In other words, the executable program file will tell you whether the default script host is CScript or WScript.
So, how do we actually get that value? Well, we first define a constant named HKEY_CLASSES_ROOT, and set its value to &H80000000; this tells the script which registry hive we want to use. Then, connect to the WMI service on the local computer (although we can easily run the script on the remote computer), be sure to bind to the root\default namespace. (Most WMI scripts use the root\cimv2 namespace, but for some reason, the system registry provider is located in Root\default.) Maybe it's a cheaper house or a better school. )
After the connection is made, we create a variable named strKeyPath and assign it a value vbsfile\shell\open\command. At this point, we are ready to read the value from the registry.
What the? Did we forget something? No, we have not forgotten anything. (Although you mention it now, we're kind of wondering if someone turned the stove off when we left the house in the morning.) You're right, in most WMI registry scripts, we need to assign values to two variables, one for the registry key and another for the values that are required in the registry key. In this case, however, we do not need to create a variable in which to store the name of the registry value. Why not? Because, technically, the (default) value doesn't actually have a name; they're just defaults. All we have to do is tell the script to read the Null value from this particular registry key. This may not sound like a good plan, but fortunately, the registry provider will know that this means reading the (default) value.
In fact, when it comes to this topic, the following line of code can retrieve the values for us:
Objregistry.getexpandedstringvalue Hkey_classes_root,strkeypath,vbnullstring,strvalue
Because "(default)" happens to have the REG_EXPAND_SZ data type, we call the GetExpandedStringValue method and pass four arguments:
? HKEY_CLASSES_ROOT a constant that tells the script which registry hive to use.
? strKeyPath the variable that tells the script which registry key to use.
? vbNullString, a VBScript constant that represents a Null value. This is where we usually insert a variable representing the value name, but as we said, our value does not have a name this time.
? strvalue, an "output" parameter. GetExpandedStringValue needs a place to store the values it reads from the registry; The output parameter is just a placeholder variable in which to store the value.
Once GetExpandedStringValue is available, we can use this line of code to change all characters in strvalue to lowercase; this is done to make it easier to determine if the value contains a special string.
strvalue = LCase (strvalue)
Finally, we call the INSTR function to see if the string value Wscript.exe can be found anywhere in the strvalue. If it can be found, we will echo back the default script host WScript; if not found, it must mean that the default script host is CScript:
Copy Code code as follows:

If InStr (strvalue, "Wscript.exe") Then
WScript.Echo "Wscript"
Else
WScript.Echo "CScript"
End If
That's it, at: Now, you can determine the default script host on your computer without running the script. (Yes, we know you have to run a script that determines the default script host ...) But you found the answer. We want you to discover the usefulness of this script and hope that everyone has learned an important lesson today: As far as you can, you can't beat the Scripting Guys.
Well, unless you ask us an unanswerable question. But we will never be stumped if we ask questions that we can answer.
Yes: almost impossible.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.