在PowerShell中显示所有站点和绑定

时间:2013-03-20 15:54:44

标签: iis powershell powershell-v3.0 iis-8

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

我希望输出是这样的:

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

8 个答案:

答案 0 :(得分:76)

试试这个:

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

答案 1 :(得分:46)

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

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

答案 2 :(得分:22)

我看到的最简单方法:

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

答案 3 :(得分:16)

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

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

cd c:\ Windows \ system32 \ inetsrv

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

appcmd list sites> C:\ IISSiteBindings.txt

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

记事本c:\ IISSiteBindings.txt

答案 4 :(得分: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
}
}

答案 5 :(得分: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
    }
}

答案 6 :(得分: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

答案 7 :(得分:0)

我发现了这个问题,因为我想生成一个网页,其中包含指向我的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>