ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
Professional ASP.NET 1.0 Special Edition
Professional ASP.NET 1.0 Special Edition

Find Prices
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software

Windows Scripting Host and IE

Written on: Nov, 30th 2001.

WSH is a administration tool. Windows Script Host is ideal for noninteractive scripting needs, such as logon scripting, administrative scripting, and machine automation. In this article, we are going to see, how WSH can be used in an Intranet Environment.

In order to run the examples discussed here, you may need the latest WSH. You can get the same from Microsoft web site by visiting the following URL http://msdn.microsoft.com/scripting And all the examples discussed here is based on the IE browser and the OS is Win 2000. You may get some different results in other OS.

While a web application is running in an intranet environment, we have control over lots of things, such as

What software should have in each clients pc?
How much memory should each clients pc have?
What browser should they use?
What should be the security settings for each browser etc etc ....

We are going to see how WSH can be used in a browser and what are the fancy things that we can do with this. To start with, let us see, how can we initialize the WScript in a browser. Well, the easiest way to create a instance of the WScript object is set WS = CreateObject("WScript.Shell"). We are basically going to discuss the methods and properties that is provided by WScript and how we can use them in an Intranet Application.

How to create Shortcuts?

We can create Shortcuts by using the method CreateShortcut. In one of the Intranet Application that I was working with, was in need of creating a Shortcut in the DeskTop of the User's PC, so that, they can click the shortcut and go to the shortcut location. This prevents the user opening the explorer and drilling down to the target location. The following example, creates a shortcut in user's Desktop. The Shortcut is nothing, but to the URL of this article.

Note: Before executing this example, make sure that, your IE browser's security allows the executing of Scripts. To run the example, make sure that, you have the following security option set to Enable.

Example: Creating Shortcuts.
 
<HTML>
<HEAD>
<TITLE>ASPAlliance: Windows Scripting Host - Tutorial</TITLE>
 
<Script Language=VBScript>
Sub Window_OnLoad()
 
Set WS = CreateObject("WScript.Shell")
Dim strDesktop
 
strDesktop = WS.SpecialFolders ("Desktop")
Set ObjSC = WS.CreateShortcut (strDesktop & "\Working with WScript.Shell.lnk")
ObjSC.TargetPath = "http://www.ASPAlliance.com/das/classes.aspx"
ObjSC.Save
 
Set ObjSC = Nothing
Set WS = nothing
End Sub
</Script>
 
</HEAD>
</HTML>

In the above example, we are extracting the physical path of "Desktop" using the property SpecialFolders. Then we are creating shortcut using the method, CreateShortCut. The TargetPath property is used to set the Target of the Shortcut and finally we save the shortcut into the Desktop. The Target path can by any thing. It can be a location to notepad.exe or calc.exe or even a path to a folder such as "C:\personal\finance\nov2001\income.xls"

Displaying System Info

We can display various system info using WSH. Following example depicts, how can we make use of WSH in retrieving various vital system info. Also we are going to learn a new method called "Popup", which is equivalent to "Msgbox" in VBScript.

Example: Displaying System Info.
 
<HTML>
<HEAD>
<TITLE>ASPAlliance: Windows Scripting Host - Tutorial</TITLE>
 
<Script Language=VBScript>
Sub Window_OnLoad()
 
Set WS = CreateObject("WScript.Shell")
Set Env = WS.Environment("SYSTEM")
 
WS.Popup Env("Number_of_Processors") ' Displays number of processor your computer has.
WS.Popup Env("OS") ' Operating system on the user's workstation.
WS.Popup Env("COMSPEC") ' Displays number of processor your computer have.
WS.Popup Env("HOMEDRIVE") ' Primary local drive (typically the C drive).
WS.Popup Env("HOMEPATH") ' Default directory for users (typically \users\default in Windows 2000).
WS.Popup Env("PATH") ' PATH environment variable.
WS.Popup Env("PATHEXT") ' Extensions for executable files (typically .com, .exe, .bat, or .cmd).
WS.Popup Env("PROMPT") ' Command prompt (typically $P$G).
WS.Popup Env("WINDIR") ' System directory (for example, c:\winnt). This is the same as SYSTEMROOT.
WS.Popup Env("TEMP") ' Directory for storing temporary files (for example, c:\temp).
 
Set Env = Nothing
Set WS = nothing
End Sub
</Script>
 
</HEAD>
</HTML>

Retrieving system info can be handy in so many cases. You may need to create some temporary files in one of your intranet App, and you may need a temporary path and you can use of the above example. All of the above information can be used in your Intranet App, provided if you are running the above example in either NT 4.0 or Win 2000.

Retrieving Environment Variables

While working on one of my Intranet project, I was in need to retrieve the value of a environment variable which exists on DOS. Initially I thought of retrieving those values from the registry, but later on I found that, we can use WSH to retrieve environment variables.

The method ExpandEnvironmentStrings can be used to retrieve the environment variable. if you want to display the contents of environment variable called %temp% you can use the following piece of code

WS.Popup WS.ExpandEnvironmentStrings("%temp%")

Reading, Writing and Deleting entries from Registry

This is my favourite. Have you ever thought of playing with registry with an HTML file? Yes, you can do what ever you want with WSH. In this coming examples, we are going to see, how we can play with registry. Before we discuss the examples, I want to make a small note that, "Working with Registry is not funny". If you unknowlingly delete some info from the registry and that may screw-up your machine. So be careful, while dealing with Registry's. To open the registry just type regedit in the command prompt or in Start->Run.

We can write a key to registry by using the method RegWrite. The following example writes a new key into your registry under HKEY_CURRENT_USER.

WS.RegWrite "HKEY_CURRENT_USER\ASPAlliance", "ASPAlliance-Welcomes-you"

We can read a key from registry by using the method RegRead. The following example reads a key (which is created in the above example) from your registry under HKEY_CURRENT_USER.

WS.Popup WS.RegRead ("HKEY_CURRENT_USER\ASPAlliance"), , "ASPAlliance"

To delete a key from registry, we should use the method RegDelete. The following example deletes the key (which we created in the previous example) from your registry under HKEY_CURRENT_USER.

WS.RegDelete "HKEY_CURRENT_USER\ASPAlliance"

Writing entries to Event Viewer

Logging the happenings during a big process will certainly help us in debugging an application. And if we have a very big application running with hundred's of users, then log will be a life saver. To open the Event Viewer type eventvwr in the command prompt or in Start->Run.

LogEvent is the method that logs an entry in to the event viewer. This method takes two arguments. The event "Type" and the actual "Descripion" of the error.

The following example, writes an error into Event Viewer WS.LogEvent 1, "File Creation Failed." Type can have any of the following values

0 SUCCESS
1 ERROR
2 WARNING
4 INFORMATION
8 AUDIT_SUCCESS
16 AUDIT_FAILURE

Summary

WSH provides another method, called Run, were you can use this to execute any windows application. you can use this method to open a notepad, calc etc etc. WS.Run "notepad" will open a notepad. Thus, WSH will be helpful to us in a lot of ways.

External Links

http://www.microsoft.com/msdownload/vbscript/scripting.asp
http://www.winguides.com/scripting/reference.php?category=3 http://www.winguides.com/registry

Send your comments to das@aspalliance.com        Back to Article list

 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 11/21/2009 6:36:50 PM