Modifying the Windows Registry on a Remote Web Server
Modifying the Windows
Registry on a Remote Web Server
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.
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.
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
I created the following ASP page, and filled in all the registry keys and values
that I needed to recreate:
‘ 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
' Remove the "\" to
make it write the actual subkey name and its value (second string)
‘ Create the “classifieds” main
‘ Begin to write all the subkeys
and their corresponding values
Data Sources\Classifieds", "Microsoft Access Driver (*.mdb)"
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,
the correct ODBC entries were restored on the web server! My classifieds section
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
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 :
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:
The syntax is:
Key or value name to write.
The value to write into the key or registry
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:
RegWrite supports strType as REG_SZ, REG_EXPAND_SZ, REG_DWORD,
and REG_BINARY. If another data type is passed as strType, RegWrite
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.