I had a question recently where someone was trying to add <clear />
or <remove />
elements to a collection in their IIS 7 configuration settings. With that in mind, for today's blog I thought that I would discuss a couple of ways to add <clear />
and <remove />
elements by using two specific scripting methods: AppCmd and VBScript.
It should be noted that you can also use JavaScript or PowerShell, but I'm not covering those because the syntax for those is available elsewhere. (JavaScript syntax is available in the Configuration Editor in IIS Manager, and the PowerShell syntax is available through the Web Server (IIS) Administration Cmdlet Reference.) You can also use Managed-Code, and the syntax for that is also available in the Configuration Editor in IIS Manager; but compiled code isn't scripting, is it? :-)
Here's the scenario, IIS makes it possible to modify the contents of an inherited collection in two ways:
- You can clear the contents of an inherited configuration section, as illustrated by the following configuration excerpt:
<configuration>
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear />
</files>
</defaultDocument>
</system.webServer>
</configuration>
- You can remove an item from an inherited collection, as illustrated by the following configuration excerpt:
<configuration>
<system.webServer>
<defaultDocument enabled="true">
<files>
<remove value="index.html" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
With that in mind, let's look at scripting those settings.
AppCmd.exe is a great utility that ships with IIS 7, which allows editing the configuration settings for IIS from a command line. This also allows you to create batch scripts that automate large numbers of configuration changes. For example, the following batch file enables ASP session state, sets the maximum number of ASP sessions to 1000, and then sets the session time-out to 10 minutes for the Default Web Site:
appcmd.exe set config "Default Web Site" -section:system.webServer/asp /session.allowSessionState:"True" /commit:apphost
appcmd.exe set config "Default Web Site" -section:system.webServer/asp /session.max:"1000" /commit:apphost
appcmd.exe set config "Default Web Site" -section:system.webServer/asp
I'm a big fan of IIS 7's AppCmd.exe, but unfortunately it has two rather large limitations:
- AppCmd.exe does not directly support clearing the contents of a configuration section. (But there's a workaround that I list below.)
- AppCmd.exe does not support removing an item from a collection.
These limitations have caused me some grief from time to time, because I often want to script the modification of collections, and I would love to remove items or clear a collection.
How to add a <clear />
element using AppCmd:
Although it's kind of a hack, there is a way to force AppCmd.exe to add a <clear />
element.
Here's what you need to do in order to clear the list of default documents for the Default Web Site:
- Create an XML file like the following and save it as "CLEAR.XML":
<?xml version="1.0" encoding="UTF-8"?>
<appcmd>
<CONFIG CONFIG.SECTION="system.webServer/defaultDocument" path="MACHINE/WEBROOT/APPHOST" overrideMode="Allow" locked="false">
<system.webServer-defaultDocument enabled="true">
<files>
<clear />
</files>
</system.webServer-defaultDocument>
</CONFIG>
</appcmd>
- Run the following command:
appcmd.exe set config /in "Default Web Site" < CLEAR.xml
Unfortunately this technique does not work for <remove />
elements. :-( But that being said, you can add a <remove />
element through VBScript; for more information, see the Using VBScript section.
Fortunately, VBScript doesn't have AppCmd.exe's limitations, so you can add both <clear />
and <remove />
elements.
How to add a <clear />
element in VBScript:
The following steps will clear the list of default documents for the Default Web Site:
- Save the following VBScript code as "clear.vbs":
Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"
Set defaultDocumentSection = adminManager.GetAdminSection("system.webServer/defaultDocument", _
"MACHINE/WEBROOT/APPHOST/Default Web Site")
Set filesCollection = defaultDocumentSection.ChildElements.Item("files").Collection
filesCollection.Clear
adminManager.CommitChanges
- Run the VBscript code by double-clicking the "clear.vbs" file.
How to add a <remove />
element in VBScript:
The following steps will remove a single item from the list of default documents for the Default Web Site:
- Save the following VBScript code as "remove.vbs":
Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"
Set defaultDocumentSection = adminManager.GetAdminSection("system.webServer/defaultDocument", _
"MACHINE/WEBROOT/APPHOST/Default Web Site")
Set filesCollection = defaultDocumentSection.ChildElements.Item("files").Collection
addElementPos = FindElement(filesCollection, "add", Array("value", "index.html"))
If (addElementPos = -1) Then
WScript.Echo "Element not found!"
WScript.Quit
End If
filesCollection.DeleteElement(addElementPos)
adminManager.CommitChanges
Function FindElement(collection, elementTagName, valuesToMatch)
For i = 0 To CInt(collection.Count) - 1
Set element = collection.Item(i)
If element.Name = elementTagName Then
matches = True
For iVal = 0 To UBound(valuesToMatch) Step 2
Set property = element.GetPropertyByName(valuesToMatch(iVal))
value = property.Value
If Not IsNull(value) Then
value = CStr(value)
End If
If Not value = CStr(valuesToMatch(iVal + 1)) Then
matches = False
Exit For
End If
Next
If matches Then
Exit For
End If
End If
Next
If matches Then
FindElement = i
Else
FindElement = -1
End If
End Function
- Run the VBscript code by double-clicking the "remove.vbs" file.
More Information
For more information about scripting and IIS configuration settings, see the following:
Note: This blog was originally posted at http://blogs.msdn.com/robert_mcmurray/