Managing ASP.Net connection strings with Powershell (1)

I came across a problem today while testing an ASP.Net application I am developing.

On my development machine, the web.config files reference both the development and the production database.

When I program a new feature into the application, I use the development database. However, to test a new feature against current database values, I have to manually switch the connection names in the application’s web.config file.

Well, I thought that could be solved by a small tool which would not take long to write.

The first decision was of course, which language would I use to implement it. Choices were C# and Powershell, and I opted for PS.

The first step was to list all available web applications on the local machine, recognizable by a web.config file.

Since the web.config file is an XML document, you can load it with the Get-Content commandlet. Listing all connection string names is simply a question of retrieving some information from the right XML nodes.

Let’s see the code:

foreach( $web in (Get-Item inetpub/wwwroot/*/web.config))
    [xml]$cfg = Get-Content $web.fullname
    foreach($node in $cfg.configuration.connectionStrings.add)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: