Manipulating IIS7 Configuration with Powershell
- by John Price
I'm trying to script some IIS7 setup tasks in PowerShell using the WebAdministration module. I'm good with setting simple properties, but am having some trouble when it comes to dealing with collections of elements in the config files.
As an immediate, example, I want to have application pools recycle on a given schedule, and the schedule is a collection of times. When I set this up via the IIS management console, the relevant config section looks like this:
<add name="CVSupportAppPool">
<recycling>
<periodicRestart memory="1024" time="00:00:00">
<schedule>
<clear />
<add value="03:31:00" />
</schedule>
</periodicRestart>
</recycling>
</add>
In my script I would like to be able to accomplish the same thing, and I have the following two lines:
#clear any pre-existing schedule
Clear-WebConfiguration "/system.applicationHost/applicationPools/add[@name='$($appPool.Name)']/recycling/periodicRestart/schedule"
#add the new schedule
Add-WebConfiguration "/system.applicationHost/applicationPools/add[@name='$($appPool.Name)']/recycling/periodicRestart/schedule" -value (New-TimeSpan -h 3 -m 31)
That does almost the same thing, but the resulting XML lacks the <clear/> element that is created with via the GUI, which I believe is necessary to avoid inheriting any default values.
This sort of collection (with "add", "remove", and "clear" elements) seems to be fairly standard in the config files, but I can't seem to find any good documentation on how to interact with them consistently.