Get dbname from multiple web.config files using powershell

I would like to publish a powershell command to return a connection string to me (in particular, I'm looking for a db name value) for all websites on a web server ...

So, I would like to see something like

site1 dbname = Northwind

site2 dbname = Fitch

site3 dbname = DemoDB

I tried using the IIS Powershell snap-in ... I thought I was close with this:

PS C: \ Windows \ system32> Get-WebApplication | Get-WebConfiguration -filter / connectionStrings / *

but ... after looking at the results ... my answer seems not to be there

I am very new to powershell - so excuse my ignorance and inexperience

Any help appreciated!

thanks!

+4
source share
3 answers

Hope this helps you get started. It just assumes that the web.config file will be on the physical path of the physical path of the web application. He does not decide to find other web.config files in the web application. It also assumes your connection strings are in the connectionStrings configuration element.

Import-Module WebAdministration Get-WebApplication | ` ForEach-Object { $webConfigFile = [xml](Get-Content "$($_.PhysicalPath)\Web.config") Write-Host "Web Application: $($_.path)" foreach($connString in $webConfigFile.configuration.connectionStrings.add) { Write-Host "Connection String $($connString.name): $($connString.connectionString)" $dbRegex = "((Initial\sCatalog)|((Database)))\s*=(?<ic>[az\s0-9]+?);" $found = $connString.connectionString -match $dbRegex if ($found) { Write-Host "Database: $($Matches["ic"])" } } Write-Host " " } 
+9
source

This post may give you an idea of ​​the beginning. Basically download the web.config file as an XML file, and then just find the node where the connection string is.

Do something like $ myFile = ([xml] Get-Content web.config). You can then pass the Get-Member message ($ myFile | Get-Member -MemberType Property) to start working in the file to find out that it has a node. I'm not on a computer where I can show you some screenshots to explain it more, but you can check this chapter from PowerShell.com "Master PowerShell", which explains working with XML very well.

0
source

How would you look for every WebApplication on every site to extract this information from every web application? I have several web applications on each IIS site, and I need to extract this information from each web.config file.

0
source

All Articles