在 powershell 中列出所有站点及其数据库名称

List all sites and their database names in powershell

我是 powershell 的新手,遇到了问题。 我正在新服务器上迁移数据库,我需要知道 IIS 上的每个网站使用哪个数据库。因为有些站点使用相同的数据库,我需要同时迁移它。 因为我有太多站点无法检查它们的连接字符串,所以我想制作 PS 脚本来列出所有站点及其数据库名称。

我已经搜索了解决方案,但找不到。 我只找到用于列出所有站点、状态、绑定的命令。 Import-Module Webadministration Get-ChildItem -Path IIS:\Sites

非常感谢。

此致, 贾卡。

此脚本可以帮助您,但是假设 web.config 文件存在,用 PowerShell 4 编写的脚本在 PowerShell 4 中进行了测试。

Import-Module WebAdministration

Get-ChildItem -Path IIS:\Sites | `
ForEach-Object {

    $webConfigFile = [xml](Get-Content "$($_.PhysicalPath)\Web.config")

    foreach($connString in $webConfigFile.configuration.connectionStrings.add)
    {
        $dbRegex = "(((Data Source)))\s*=(?<ic>[a-z\s0-9]+?);"
        $found = $connString.connectionString -match $dbRegex
        if ($found)
        {
            Write-Host "Database: $($Matches["ic"])"
        }
    }}