Skip to content

Programing IIS website configuration properties

Where possible it is easiest to manage IIS web configuration properties for applications via the web config file, for ASP.NET web applications at least. However, while setting up automated deployments recently I came across a situation whereby the values in the <sessionState> needed to change per environment. I will point out that I think using session state is a bad idea but sometimes needs must.

As I was using Octopus, my first approach was to “#{variable}” up the configuration. However, it soon became a pain to run locally as I had to consciously make sure that this wasn’t being overwritten on check-in. I also had to change it constantly for debugging. I could have used web config transforms, but I wanted something that I could use outside of ASP web applications and that was a bit more flexible.

What I discovered is that there is a remarkably easy PowerShell solution. This can be run after the IIS site has been set up as a deployment step, and everything will work smoothly.

Set-WebConfigurationProperty system.web/sessionState IIS:\Sites\#{WebsiteName} -Name mode -Value StateServer
Set-WebConfigurationProperty system.web/sessionState IIS:\Sites\#{WebsiteName} -Name stateConnectionString -Value #{SessionStateServer}

What this does is change the IIS configuration properties of the site specified, for the name specified, with the values specified.

Session state isn’t the only thing that this can apply to. “Set-WebConfigurationProperty” should work for most things that you can specify in the <system.web> namespace of the ASP.NET web config file (I think).

If you use Octopus, this is especially useful as Octopus only lets you use variable replacement on app settings and connection strings. This lets you expand that reach just a little bit further.

Published inOperations

Comments are closed.