使用Powershell从多个web.config文件中获取dbname [英] Get dbname from multiple web.config files with powershell
问题描述
我想发出一个powershell命令向我返回Web服务器上所有网站的连接字符串(特别是我正在寻找的db name值)...
I would like to issue a powershell command to return me the connection string (specifically I am looking for the db name value) for all the web sites on a web server...
所以我想看到类似
site1 dbname = Northwind
site1 dbname=Northwind
site2 dbname = Fitch
site2 dbname=Fitch
site3 dbname = DemoDB
site3 dbname=DemoDB
我尝试使用IIS Powershell管理单元...我以为我对此很满意:
I have tried using the IIS Powershell snap-in... I thought I was close with this:
PS C:\ Windows \ system32>获取Web应用程序|Get-WebConfiguration -filter/connectionStrings/*
PS C:\Windows\system32> Get-WebApplication | Get-WebConfiguration -filter /connectionStrings/*
但是...查看结果后...我的答案似乎不在那儿
but... after looking at the results... my answer doesn't appear to be in there
我对Powershell非常陌生-请原谅我的无知和经验不足
I am very new to powershell - so excuse my ignornance and inexperience
任何帮助表示赞赏!
谢谢!
推荐答案
希望这会帮助您入门.这只是假设Web应用程序的物理路径的物理路径上将存在一个web.config文件.它不会递归在Web应用程序中找到其他web.config文件.它还假定您的连接字符串在connectionStrings配置元素中.
Hopefully, this will get you started. This just assumes there will be a web.config file at the physical path of the web application's physical path. It does not recurse 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>[a-z\s0-9]+?);"
$found = $connString.connectionString -match $dbRegex
if ($found)
{
Write-Host "Database: $($Matches["ic"])"
}
}
Write-Host " "
}
这篇关于使用Powershell从多个web.config文件中获取dbname的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!