58

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

我希望输出是这样的:

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

8 回答 8

97

试试这个:

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 语句的管道不会给你你需要的东西。根据您的要求,我将构建一个自定义对象或哈希表。

于 2013-03-20T16:21:25.150 回答
72

尝试这样的事情来获得你想要的格式:

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
于 2013-03-20T16:34:52.827 回答
26

我看到的最简单的方法:

Foreach ($Site in get-website) { Foreach ($Bind in $Site.bindings.collection) {[pscustomobject]@{name=$Site.name;Protocol=$Bind.Protocol;Bindings=$Bind.BindingInformation}}}
于 2017-02-08T12:43:35.893 回答
25

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

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

cd c:\Windows\system32\inetsrv

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

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

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

记事本 c:\IISSiteBindings.txt

于 2016-02-02T16:20:06.670 回答
2

试试这个

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
}
}
于 2015-04-13T23:56:28.190 回答
2
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
    }
}
于 2016-09-07T10:12:16.583 回答
2

我找到此页面是因为我需要将具有许多绑定的站点迁移到新服务器。我在这里使用了一些代码来生成下面的 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
于 2020-01-22T09:32:18.953 回答
0

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

$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>
于 2018-02-13T20:14:43.890 回答