Iis 使用powershell从多个web.config文件中获取dbname

Iis 使用powershell从多个web.config文件中获取dbname,iis,powershell,web-config,Iis,Powershell,Web Config,我想发出powershell命令以返回web服务器上所有网站的连接字符串(特别是我正在查找db name值) 所以我想看看像这样的东西 site1 dbname=Northwind site2 dbname=Fitch site3 dbname=demodhb 我已尝试使用IIS Powershell管理单元。。。我想我已经接近这个目标了: PS C:\Windows\system32>Get-WebApplication | Get-WebConfiguration-filter/Connec

我想发出powershell命令以返回web服务器上所有网站的连接字符串(特别是我正在查找db name值)

所以我想看看像这样的东西

site1 dbname=Northwind

site2 dbname=Fitch

site3 dbname=demodhb

我已尝试使用IIS Powershell管理单元。。。我想我已经接近这个目标了:

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

但是。。。在看了结果之后。。。我的答案似乎不在里面

我对powershell非常陌生-请原谅我的无知和缺乏经验

感谢您的帮助

谢谢

这可能会给你一个开始的想法。基本上,将web.config文件作为XML文件加载,然后找到连接字符串所在的节点


执行类似$myFile=([xml]getcontentweb.config)的操作。然后,您可以通过管道将其传递给成员($myFile | Get Member-MemberType Property),以开始在文件中工作,查看哪个节点拥有它。我不在电脑前,我可以向您展示一些屏幕截图来进一步解释它,但您可以从PowerShell.com的“Master PowerShell”电子书中查看这一点,该电子书非常好地解释了如何使用XML。

希望这能让您开始学习。这只是假设在web应用程序的物理路径的物理路径中将有一个web.config文件。它不会在web应用程序中递归查找其他web.config文件。它还假定您的连接字符串位于ConnectionString配置元素中

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 " "
}
导入模块管理
获取WebApplication|`
ForEach对象{
$webConfigFile=[xml](获取内容“$($.PhysicalPath)\Web.config”)
写入主机“Web应用程序:$($\路径)”
foreach($webConfigFile.configuration.ConnectionString.add中的connString)
{
写入主机“连接字符串$($connString.name):$($connString.connectionString)”
$dbRegex=“((初始\sCatalog)|((数据库)))\s*=(?[a-z\s0-9]+?)”
$found=$connString.connectionString-匹配$dbRegex
如有($已找到)
{
写入主机“数据库:$($Matches[“ic”])”
}
}
写入主机“”
}

完美!非常感谢你。感谢您的帮助不要忘记将最有用的答案标记为“正确”。