【问题标题】:Display all sites and bindings in PowerShell在 PowerShell 中显示所有站点和绑定
【发布时间】:2013-03-20 15:54:43
【问题描述】:

我正在从 IIS 记录所有与该站点相关的站点和绑定。有没有一种简单的方法可以通过 PowerShell 脚本而不是手动输入查看 IIS 来获取此列表?

我希望输出是这样的:

Site                          Bindings
TestSite                     www.hello.com
                             www.test.com
JonDoeSite                   www.johndoe.site

【问题讨论】:

  • 你有什么版本的 Windows/IIS?如果我没记错的话,在 Windows Server 2012 上使用 IIS,您可以简单地使用 Get-WebBinding
  • 是的,它是 Server 2012。运行 Get-Webbinding 会返回 protocal、bindinginformaiton、sslFlags。与我想要的输出格式不同。

标签: iis powershell powershell-3.0 iis-8


【解决方案1】:

试试这个:

Import-Module Webadministration
Get-ChildItem -Path IIS:\Sites

它应该返回如下所示的内容:

Name             ID   State      Physical Path                  Bindings
----             --   -----      -------------                  --------
ChristophersWeb 22   Started    C:\temp             http *:8080:ChristophersWebsite.ChDom.com

您可以从这里优化结果,但要小心。 select 语句的管道不会给你你需要的东西。根据您的要求,我将构建一个自定义对象或哈希表。

【讨论】:

  • 它是如何工作的?我的意思是它可以工作,但无论我的文件位于何处? IIS:\Sites 将始终从 IIS 中选择项目?或者这是否与规范有关。路径?
  • @RayofCommand - 这通过解析 IIS Web 配置文件中定义的位置来工作,这些文件取代了 IIS 5/6 元数据库。这些本质上是 xml 文件,位于 C:\Windows\System32\inetsrv\config
  • 至少在 Windows 2008 R2 SP1 中您必须先调用 Set-ExecutionPolicy RemoteSigned 否则 Import-Module 将不起作用。并且您需要将参数放在引号中,例如:Get-ChildItem -Path "IIS:\Sites",否则您会收到语法错误。
  • 这不应该发生,模块已签名。我会研究那个。 @Abel
  • Get-Website 基本上做同样的事情,但更短一些。
【解决方案2】:

尝试这样的方法来获得你想要的格式:

Get-WebBinding | % {
    $name = $_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', '$1'
    New-Object psobject -Property @{
        Name = $name
        Binding = $_.bindinginformation.Split(":")[-1]
    }
} | Group-Object -Property Name | 
Format-Table Name, @{n="Bindings";e={$_.Group.Binding -join "`n"}} -Wrap

【讨论】:

  • 非常好。我不会考虑添加正则表达式来评估对象,但这是对 New\Group-Object cmdlet 的很好使用。鉴于此,我将不得不更新一些代码。
  • 使用 powershell.exe 调用 myscript.ps1,Format-Table 无法显示(输出)
  • 在 Windows 2008 R2 SP1 中,这会引发错误 “术语 'Get-WebBinding' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查拼写的名称,或者如果包含路径,请验证路径是否正确,然后重试。”(如果稍微更改,下面的第二个答案有效)。
  • 问题的第一条评论中已经提到了:)
  • Awesome Script 只需要做一件事。我们必须以管理员身份运行 powershell
【解决方案3】:

我看到的最简单的方法:

Foreach ($Site in get-website) { Foreach ($Bind in $Site.bindings.collection) {[pscustomobject]@{name=$Site.name;Protocol=$Bind.Protocol;Bindings=$Bind.BindingInformation}}}

【讨论】:

  • 很好的解决方案,Alexander!,我刚刚添加了物理路径:Foreach ($Site in get-website) { Foreach ($Bind in $Site.bindings.collection) { [pscustomobject]@{name=$Site.name;Protocol=$Bind.Protocol;Bindings=$Bind.BindingInformation; path=$Site.physicalPath }}}
  • 这对我有用。太感谢了。您可能需要以管理员身份运行 Powershell 才能执行此脚本,否则您可能会收到一个错误提示您应该具有提升的状态才能访问 IIS 配置数据
【解决方案4】:

如果您只想列出所有站点(即查找绑定)

将工作目录更改为“C:\Windows\system32\inetsrv”

cd c:\Windows\system32\inetsrv

接下来运行“appcmd list sites”(复数)并输出到文件。例如 c:\IISSiteBindings.txt

appcmd 列出站点> c:\IISSiteBindings.txt

现在在命令提示符下使用记事本打开。

记事本 c:\IISSiteBindings.txt

【讨论】:

  • 不是 PS 答案;但简短而甜美,还列出了可用于扫描 IIS 日志的 ID 号。
  • 对于那些运行非常旧版本的 Windows Server 的人来说,这是最好的答案。 Windows 服务器 6.0(内部版本 6003:Service Pack 2)。请记住,您可能需要使用管理权限启动命令提示符。
  • 作为一个班轮:c:\Windows\system32\inetsrv\appcmd list sites
【解决方案5】:

试试这个

function DisplayLocalSites
{

try{

Set-ExecutionPolicy unrestricted

$list = @()
foreach ($webapp in get-childitem IIS:\Sites\)
{
    $name = "IIS:\Sites\" + $webapp.name
    $item = @{}

$item.WebAppName = $webapp.name

foreach($Bind in $webapp.Bindings.collection)
{
    $item.SiteUrl = $Bind.Protocol +'://'+         $Bind.BindingInformation.Split(":")[-1]
}


$obj = New-Object PSObject -Property $item
$list += $obj
}

$list | Format-Table -a -Property "WebAppName","SiteUrl"

$list | Out-File -filepath C:\websites.txt

Set-ExecutionPolicy restricted

}
catch
{
$ExceptionMessage = "Error in Line: " + $_.Exception.Line + ". " +     $_.Exception.GetType().FullName + ": " + $_.Exception.Message + " Stacktrace: "    + $_.Exception.StackTrace
$ExceptionMessage
}
}

【讨论】:

  • 这个仅显示我在每个 IIS:\Sites\ 中的第一个域,而不是我在同一站点中配置的所有其他域名绑定。
【解决方案6】:
function Get-ADDWebBindings {
param([string]$Name="*",[switch]$http,[switch]$https)
    try {
    if (-not (Get-Module WebAdministration)) { Import-Module WebAdministration }
    Get-WebBinding | ForEach-Object { $_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', '$1' } | Sort | Get-Unique | Where-Object {$_ -like $Name} | ForEach-Object {
        $n=$_
        Get-WebBinding | Where-Object { ($_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', '$1') -like $n } | ForEach-Object {
            if ($http -or $https) {
                if ( ($http -and ($_.protocol -like "http")) -or ($https -and ($_.protocol -like "https")) ) {
                    New-Object psobject -Property @{Name = $n;Protocol=$_.protocol;Binding = $_.bindinginformation}
                }
            } else {
                New-Object psobject -Property @{Name = $n;Protocol=$_.protocol;Binding = $_.bindinginformation}
            }
        }
    }
    }
    catch {
       $false
    }
}

【讨论】:

    【解决方案7】:

    我找到这个页面是因为我需要将一个包含许多绑定的站点迁移到一个新服务器。我在这里使用了一些代码来生成下面的 powershell 脚本,以将绑定添加到新服务器。分享,以防对他人有用:

    Import-Module WebAdministration
    $Websites = Get-ChildItem IIS:\Sites
    $site = $Websites | Where-object { $_.Name -eq 'site-name-in-iis-here' }
    
    $Binding = $Site.bindings
    [string]$BindingInfo = $Binding.Collection
    [string[]]$Bindings = $BindingInfo.Split(" ")
    $i = 0
    $header = ""
    Do{
        [string[]]$Bindings2 = $Bindings[($i+1)].Split(":")
    
        Write-Output ("New-WebBinding -Name `"site-name-in-iis-here`" -IPAddress " + $Bindings2[0] + " -Port " + $Bindings2[1] + " -HostHeader `"" + $Bindings2[2] + "`"")
    
        $i=$i+2
    } while ($i -lt ($bindings.count))
    

    它会生成如下所示的记录:

    New-WebBinding -Name "site-name-in-iis-here" -IPAddress "*" -Port 80 -HostHeader www.aaa.com
    

    【讨论】:

      【解决方案8】:

      我发现这个问题是因为我想生成一个网页,其中包含指向在我的 IIS 实例上运行的所有网站的链接。我使用Alexander Shapkin's answer 想出以下来生成一堆链接。

      $hostname = "localhost"
      
      Foreach ($Site in get-website) {
          Foreach ($Bind in $Site.bindings.collection) {
              $data = [PSCustomObject]@{
                  name=$Site.name;
                  Protocol=$Bind.Protocol;
                  Bindings=$Bind.BindingInformation
              }
              $data.Bindings = $data.Bindings -replace '(:$)', ''
              $html = "<a href=""" + $data.Protocol + "://" + $data.Bindings + """>" + $data.name + "</a>"
              $html.Replace("*", $hostname);
          }
      }
      

      然后我将结果粘贴到这个仓促编写的 HTML 中:

      <html>
      <style>
          a { display: block; }
      </style>
      {paste PowerShell results here}
      </body>
      </html>
      

      【讨论】:

      • 我想我可以在脚本中包含 HTML,而无需粘贴。
      猜你喜欢
      • 1970-01-01
      • 2020-01-06
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2019-04-20
      • 2017-10-04
      • 1970-01-01
      相关资源
      最近更新 更多