Modifying the Windows Registry on a Remote Web Server
           using ASP and the Windows Script Host

By Peter A. Bromberg, Ph.D.

Windows Script, as we have recently seen, can be used destructively, and that’s unfortunate. We simply have to guard against potential abuses and take the appropriate advance steps to make sure that we are safe.   However, as developers, I am sure you will agree that the advantages that scripting provides far outweigh the potential dangers.

Here is a real – world example of how to use the Windows Scripting Host with VBScript in an ASP Page that can be run on a remote web server by simply “loading the URL” into your browser. I hope it provides “food for thought” and a basis for you to be able to solve some of your own problems.



Recently, one of the web sites I run on a friend’s machine got messed up. Somebody removed an old web site folder. Unfortunately, the folder also contained an Access database that was being used by one of my active sites in some Cold Fusion pages. I FTP’ed a new copy of the MDB file but soon discovered that somehow the ODBC System DSN had also been removed.  My friend was away and I didn’t know when he would return, and I had no “desktop” access to the web server, only FTP access.

Rather than being faced with the embarrassment of having a web site whose “classifieds” section would show all kinds of errors, here’s how I solved the problem:

First, I created a new ODBC System DSN on my own machine so I could export all the registry entries.  To export registry entries, run REGEDIT, highlight the key you want to export, and choose “Registry/ Export Registry File” from the Main Menu.  The result is a text file with a “.reg” extension.  You can execute this by double clicking on it to enter the items contained therein into the registry. And, you can also copy the contents into an ASP page and have the Windows Script Host enter them remotely!

Second, I created the following ASP page, and filled in all the registry keys and values that I needed to recreate:

<%

Dim WSHShell

‘ Create an instance of the Windows Script Host “Shell” Object

Set WSHShell = CreateObject("WScript.Shell")

' IMPORTANT! When first string has a "\" at end, this creates the KEY itself, not the key or subkey value!

' Remove the "\" to make it write the actual subkey name and its value (second string)

‘ Create the “classifieds” main key

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\", "Default"

‘ Begin to write all the subkeys and their corresponding values

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\Driver", "C:\WINDOWS\SYSTEM\odbcjt32.dll"

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\DBQ", "C:\CFUSION\database\classifieds.mdb"

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\DriverId", 00000019, "REG_DWORD"

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\FIL" ,"MS Access;"

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\SafeTransactions" ,00000000, "REG_DWORD"

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\UID" ,""

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\ODBC Data Sources\Classifieds", "Microsoft Access Driver (*.mdb)"

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\Engines\" , ""

WSHShell.regWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\Engines\Jet\", ""

WSHShell.regWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\Engines\Jet\ImplicitCommitSync", ""

WSHShell.regWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\Engines\Jet\MaxBufferSize", 00000800, "REG_DWORD"

WSHShell.regWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\Engines\Jet\PageTimeout", 00000005, "REG_DWORD"

WSHShell.regWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\Engines\Jet\Threads", 00000003, "REG_DWORD"

WSHShell.regWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\Engines\Jet\UserCommitSync" ,"Yes"

Response.write "Registry Modifications Complete!"

%>

When I FTP’ed the above “RegistryEdit.asp” page to my webspace on my friend’s machine, and opened my browser with http://www.myserver.com/RegistryEdit.asp, Voila!

All the correct ODBC entries were restored on the web server! My classifieds section worked great!

The key here is to understand the syntax involved in getting the difference between creating a registry “KEY”, and entering a key or subkey “VALUE”. The documentation (and example)  from Microsoft on the Wscript.Shell  “Regwrite”  syntax is relatively poor. Here’s the catch:

When you want to CREATE a new key (or just make sure that it’s already there), you place a backslash (‘\”) at the end of the first string in the “Regwrite” method line.  So if you look at my script, the first thing I needed to do was to recreate the “classifieds” KEY. I did this with :

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\", "Default"

I put “Default” in there; you can put an empty string (“”) if you like. The Important thing to remember is, the first string after the WSHShell.RegWrite directive is terminated in a backslash when you want to create a key instead of write a key value.

Now that I have made sure the “HKLM\Software\ODBC\ODBC.INI\classifieds\” key is there, I am ready to write the subkeys and their values there, like this:

WSHShell.RegWrite "HKLM\Software\ODBC\ODBC.INI\classifieds\Driver", "C:\WINDOWS\SYSTEM\odbcjt32.dll"

The syntax is:

object.RegWrite strName, anyValue [,strType]

object

WshShell object.

strName

Key or value name to write.

anyValue

The value to write into the key or registry value.

strType

Optional. The data type for the value being stored in the registry.

If strName ends with the backslash character (\), this method returns (creates) the key instead of the value. StrName must begin with one of following root key names:

Short

Long

HKCU

HKEY_CURRENT_USER

HKLM

HKEY_LOCAL_MACHINE

HKCR

HKEY_CLASSES_ROOT

 

HKEY_USERS

 

HKEY_CURRENT_CONFIG

RegWrite supports strType as REG_SZ, REG_EXPAND_SZ, REG_DWORD, and REG_BINARY. If another data type is passed as strType, RegWrite returns E_INVALIDARG

Of course, you can also read registry values from within as ASP script or instantiated COM component by using the corresponding “RegRead” method. I have found this process to be reasonably fast, and that means we have an alternative method of storing state in the registry, rather than relying on cookies, ASP session variables, the querystring, hidden form fields or a database.  Of course, if you have a lot of users and many keys and values to write, you could create a really big registry, so you have to think things through. But it’s a nice tool to know about.

This should give you the basics of what it takes to write to the Windows registry remotely using ASP with VBScript and the Windows Scripting Host Shell Object.