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))
{
$web.fullname
[xml]$cfg = Get-Content $web.fullname
foreach($node in $cfg.configuration.connectionStrings.add)
{
$node.name
}
write-host
}