繁体   English   中英

使用Powershell从web.config中提取dbname

[英]Pulling dbname from web.config using powershell

在这里找到了我的答案的一部分, 使用powershell从多个web.config文件中获取dbname

但是需要它来遍历IIS站点和这些“站点”内的所有Web应用程序。 我在每个“站点”中都有多个带有多个Web应用程序的IIS“站点”,需要检查每个web.config文件并提取使用的数据库。 不能弄清楚如何递归上面链接中的代码。

    #Code from link above.

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 " "
}

希望这样为每个IIS站点和IIS站点中的每个Web应用程序输出每个web.config文件的数据库名称。 当前,它仅查看IIS站点中的第一个Web应用程序,而不查看IIS站点中的其他任何Web应用程序,还查看IIS站点的web.config是否具有与数据库的连接字符串。

这是为您提供的代码,从您的参考中我更改了循环机制,而不是获取数据库名称的实际逻辑。

Import-Module WebAdministration

Get-Website | % {

    $AppList = Get-WebApplication -Site $_.Name 
    foreach ( $app in $AppList )
    {
        $app.PhysicalPath
        $webConfigFile = [xml](Get-Content "$($app.PhysicalPath)\Web.config")

        # NEED TO CHECK WEATHER THE $webConfigFile  FILE EXIST OR NOT

        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"])"
            }
        }

    } # END OF INNER FOR EACH LOOP

} # END OF OUTER FOR EACH LOOP

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM