Simple IIS Scripting
2009
Using the Windows Scripting Host, it’s possible to write small JScript files which perform administrative tasks. I had a need for a small script to automatically create a virtual directory in IIS and link it with a particular path as part of a (very basic) deployment procedure. After a little bit (maybe 15 minutes?) of searching and reading, this is what I ended up with :
function install() { var hostname = "localhost"; var projectName = "MyProject"; var projectDir = "C:\\Projects\\MyProject"; try { var existing = GetObject("IIS://" + hostname + "/W3SVC/1/Root/" + projectName); WScript.Echo("ERROR : An application called " + projectName + " already exists."); return; } catch(e) {} try { //Get the root IIS application var objIIS = GetObject("IIS://" + hostname + "/W3SVC/1/Root"); var virtualDir = objIIS.Create("IISWebVirtualDir", projectName); virtualDir.AppIsolated = 2; //pooled process virtualDir.AccessScript = true; virtualDir.AppFriendlyName = projectName; virtualDir.Path = projectDir; virtualDir.AccessWrite = true; virtualDir.AccessRead = true; virtualDir.AccessExecute = true; virtualDir.AppCreate(true); virtualDir.SetInfo(); WScript.Echo("IIS application \"" + projectName + "\" created successfully."); } catch(e) { var msg = ""; for (var prop in e) { msg += prop + ":" + e[prop] + "\n"; } WScript.Echo(msg); } } install();
Pretty simple really. The WScript object is the root of the WSH hierarchy and is always available, but the important type here is IISWebVirtualDir.
It’s possible to do all kinds of things in IIS from a script, but this was all I needed. I might investigate permissions and DNS entries if the deployment gets more complicated (or more frequent).
Comment