[英]Get dbname from multiple web.config files with powershell
我想發出一個 powershell 命令為我返回 web 服務器上所有 web 站點的連接字符串(特別是我正在尋找數據庫名稱值)...
所以我想看到類似的東西
site1 dbname=Northwind
site2 dbname=Fitch
site3 dbname=DemoDB
我曾嘗試使用 IIS Powershell 管理單元...我以為我很接近這個:
PS C:\Windows\system32> Get-WebApplication | 獲取 WebConfiguration -filter /connectionStrings/*
但是...查看結果后...我的答案似乎不在其中
我對 powershell 很陌生-請原諒我的無知和缺乏經驗
任何幫助表示贊賞!
謝謝!
希望這能讓你開始。 這只是假設在 web 應用程序的物理路徑的物理路徑中會有一個 web.config 文件。 在 web 應用程序中查找其他 web.config 文件不會遞歸。 它還假設您的連接字符串位於 connectionStrings 配置元素中。
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 " "
}
這篇文章可能會給你一個開始的想法。 基本上將 web.config 文件加載為 XML 文件,然后找到連接字符串所在的節點。
執行類似 $myFile = ([xml] Get-Content web.config) 的操作。 然后,您可以 pipe 到 Get-Member ( $myFile | Get-Member -MemberType 屬性) 開始進入文件以查看哪個節點擁有它。 我不在電腦前,我可以向您展示一些屏幕截圖以進行更多解釋,但您可以從 PowerShell.com “Master PowerShell”電子書中查看本章,它很好地解釋了如何使用 Z3501BB093D363810B671059B9CFED。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.