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

发布于 2024-11-29 05:31:14 字数 427 浏览 1 评论 0原文

我想发出一个 powershell 命令来返回网络服务器上所有网站的连接字符串(特别是我正在寻找数据库名称值)...

所以我想看到类似

site1 dbname=Northwind

site2 的 内容dbname=Fitch

site3 dbname=DemoDB

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

PS C:\Windows\system32>获取 Web 应用程序 | Get-WebConfiguration -filter /connectionStrings/*

但是...查看结果后...我的答案似乎不在那里

我对 powershell 很陌生 - 所以请原谅我的无知和缺乏经验

任何帮助表示赞赏!

谢谢!

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...

So I would like to see something like

site1 dbname=Northwind

site2 dbname=Fitch

site3 dbname=DemoDB

I have tried using the IIS Powershell snap-in... I thought I was close with this:

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

I am very new to powershell - so excuse my ignornance and inexperience

Any help appreciated!

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕色琉璃 2024-12-06 05:31:14

希望这能让您开始。这只是假设 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 " "
}

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 " "
}
我还不会笑 2024-12-06 05:31:14

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

执行类似 $myFile = ([xml] Get-Content web.config) 的操作。然后,您可以将其通过管道传递给 Get-Member ( $myFile | Get-Member -MemberType 属性),开始进入文件以查看哪个节点拥有该文件。我不在电脑前,无法向您展示一些屏幕截图来进一步解释,但您可以查看此 章节 来自 PowerShell.com“Master PowerShell”电子书,很好地解释了 XML 的使用。

This post may give you an idea to start with. Basically load in the web.config file as an XML file and then just find the node where the connection string is.

Do something like $myFile = ([xml] Get-Content web.config). You can then pipe that to Get-Member ( $myFile | Get-Member -MemberType Property) to start working your way into the file to see what node has it. I'm not at a computer where I can show you some screenshots to explain it more, but you can check this chapter out from PowerShell.com "Master PowerShell" e-book that explains working with XML very well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文