Scripts can even be run from batch files
or a command prompt by running the program CSCRIPT. EXE with the script file
name as the first parameter to the command. Functionality specific to the
Windows Scripting Host is accessed using its primary object WScript. This object
is always present, and so does not have to be created. It provides several
important methods such as CreateObject which is used to create instances of
other objects, and Quit which allows you to terminate a script before the end of
the script file. The Windows script (.wsf) file, a text document
containing Extensible Markup Language (XML) code, incorporates several
features that offer you increased scripting flexibility. Because Windows
script files are not engine-specific, they can contain script from any
ActiveX®-compliant scripting engine. With .wsf files, you can take advantage of the
following benefits as you create your scripts:
- Support for Include statements - allows you
to incorporate previously-written functions from VBScript or JScript files
into your Windows Script Host project.
- Support for multiple engines - allows you to
use more than one scripting language per file.
- Support for Type libraries - allows you to
add constants to your code.
- Support for tools - allows you to edit files
with any XML editor.
- Support for multiple jobs in one file -
allows you to store all of your code in a single location.
Include Statements
If you have .js and .vbs files from previous
Windows Script Host projects, a .wsf file enables you to use them with Windows
Script Host. A .wsf file encapsulates a library of functions that can in turn
be used by multiple .wsf files.
The following example shows the contents of a .wsf
file that includes a JScript file (fso.js), plus a VBScript function that
calls a function (GetFreeSpace) in the included file. The contents of fso.js
are provided also.
<Job id="IncludeExample">
<script language="JScript" src=_quot;FSO.JS_quot;/_gt;
_lt;script language="VBScript">
' Get the free space for drive C.
s = GetFreeSpace("c:")
WScript.Echo s
</Script>
</Job>
Contents of fso.js:
function GetFreeSpace(drvPath) {
var fs, d, s;
fs = new ActiveXObject("Scripting.FileSystemObject");
d = fs.GetDrive(fs.GetDriveName(drvPath));
s = "Drive " + drvPath + " - " ;
s += d.VolumeName;
s += " Free Space: " + d.FreeSpace/1024 + " Kbytes";
return s;
}
Multiple-Engine Support
Since one scripting language may not have all
the functionality you need, Windows Script Host allows you to combine multiple
languages in a single .wsf file. The following example shows the contents of a
.wsf file that includes both VBScript® and PerlScript code:
<job id="PERLandVBS">
<script language=PerlScript RUNAT=Server>
sub PerlHello
{
my $str = @_[0];
$WScript->Echo($str);
}
</script>
<script language="VBScript">
WScript.Echo "Hello from VBScript"
PerlHello "Hello from PERLScript"
</Script>
</Job>
Type Library Support
In the following example, MyComponent was
developed with Microsoft® Visual Basic® 5.0. MyComponent defines the constant
MyError with the following statement.
Public Const MyError = "You are not using MyComponent correctly."
The type library is contained in
mycomponent.lib, which you have installed in C:\MyComponent.
<Job id="IncludeExample">
<Reference progid="MyComponent.MyClass">
<Script language="VBScript">
Dim MyVar
Set MyVar = CreateObject("MyComponent.MyClass")
Currentreturn = MyVar.MyMethod
If Currentreturn = False then
WScript.Echo MyError
End If
</Script>
</Job>
Sample scripts for checking the
system configuration and installed softwares.
Type this script code in a text file and save as .vbs
file.
Registration_Details.vbs
Set dtmConvertedDate =
CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
Wscript.Echo "Operating System: " & objOperatingSystem.Caption
dtmConvertedDate.Value = objOperatingSystem.InstallDate
dtmInstallDate = dtmConvertedDate.GetVarDate
Wscript.Echo "Install Date: " & dtmInstallDate
Wscript.Echo "Licensed Users: " & _
objOperatingSystem.NumberOfLicensedUsers
Wscript.Echo "Organization: " & objOperatingSystem.Organization
Wscript.Echo "Version: " & objOperatingSystem.Version
Next
version.vbs
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFeatures = objWMIService.ExecQuery _
("Select * from Win32_SoftwareFeature")
For Each objFeature in colfeatures
Wscript.Echo "Product : " & objFeature.ProductName & "---" & "Version : " &
objFeature.Version
Next
components.vbs
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_ApplicationService")
For Each objItem in colItems
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "Start Mode: " & objItem.StartMode
Wscript.Echo
Next
installedsoftwares.vbs
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
strComputer = "."
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
strEntry1a = "DisplayName"
strEntry1b = "QuietDisplayName"
strEntry2 = "InstallDate"
strEntry3 = "VersionMajor"
strEntry4 = "VersionMinor"
strEntry5 = "EstimatedSize"
Set objReg = GetObject("winmgmts://" & strComputer & _
"/root/default:StdRegProv")
objReg.EnumKey HKLM, strKey, arrSubkeys
WScript.Echo "Installed Applications" & VbCrLf
For Each strSubkey In arrSubkeys
intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, _
strEntry1a, strValue1)
If intRet1 <> 0 Then
objReg.GetStringValue HKLM, strKey & strSubkey, _
strEntry1b, strValue1
End If
If strValue1 <> "" Then
WScript.Echo VbCrLf & "Display Name: " & strValue1
End If
objReg.GetStringValue HKLM, strKey & strSubkey, _
strEntry2, strValue2
If strValue2 <> "" Then
WScript.Echo "Install Date: " & strValue2
End If
objReg.GetDWORDValue HKLM, strKey & strSubkey, _
strEntry3, intValue3
objReg.GetDWORDValue HKLM, strKey & strSubkey, _
strEntry4, intValue4
If intValue3 <> "" Then
WScript.Echo "Version: " & intValue3 & "." & intValue4
End If
objReg.GetDWORDValue HKLM, strKey & strSubkey, _
strEntry5, intValue5
If intValue5 <> "" Then
WScript.Echo "Estimated Size: " & Round(intValue5/1024, 3) & " megabytes"
End If
Next
Back To Articles |