2013-03-20 47 views
29

我正在記錄所有網站和綁定相關的網站從IIS。想知道是否有一種簡單的方法通過PowerShell腳本獲取此列表,而不是手動輸入查看IIS?顯示powershell中的所有網站和綁定

我想要的輸出是這樣的:

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

你有什麼版本的Windows/IIS?使用Windows Server 2012上的IIS,如果我沒有記錯,只需使用「Get-WebBinding」即可。 – 2013-03-20 16:07:17

+0

是的,它是Server 2012.運行Get-Webbinding返回協議,bindinginformaiton,sslFlags。不是我想要我的輸出格式。 – sanjeev40084 2013-03-20 16:09:19

回答

28

嘗試這樣的事情,讓你想要的格式:(即找到一個結合)

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

非常好。我不會考慮添加正則表達式來評估對象,但這非常適合使用New \ Group-Object cmdlet。鑑於此,我將不得不更新一些代碼。 – 2013-03-20 16:41:41

+0

使用powershell.exe調用myscript.ps1,Format-Table無法正常顯示(輸出) – Kiquenet 2014-10-17 08:30:54

+1

在Windows 2008 R2 SP1中,這會引發錯誤_「術語'Get-WebBinding'不被識別爲cmdlet的名稱,功能,腳本文件或可操作程序,檢查名稱的拼寫,或者如果包含路徑,請確認路徑是正確的,然後再試一次。「_(如果適度更改,以下第二個答案)。 – Abel 2016-06-16 09:40:16

52

試試這個

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語句的管道不會給你你需要的東西。基於你的要求,我會建立一個自定義對象或哈希表。

+0

工程。真棒:) – sanjeev40084 2013-03-20 16:29:47

+0

它是如何工作的?我的意思是它的作品,但無論我在哪裏我的文件位於? IIS:\ Sites將始終從IIS中選擇項目?或者是綁定到一個規範。路徑? – RayofCommand 2014-06-23 10:56:54

+1

@RayofCommand - 通過解析IIS web配置文件中定義的位置來解決這個問題,這些文件取代了IIS 5/6元數據庫。這些是本質上的xml文件,位於C:\ Windows \ System32 \ inetsrv \ config – 2014-07-26 02:58:24

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 
} 
} 
+0

的身份運行PowerShell,它只顯示每個IIS中的我的第一個域:\ Sites \,而不是我在同一站點內配置的所有其他域名綁定。 – 2016-12-28 19:15:50

9

如果你只是想列出所有的網站

將工作目錄更改爲C:\ Windows \ system32 \ inetsrv

cd c:\ Windows \ system32 \ inetsrv

接下來運行appcmd列表站點並輸出到文件。 e.g C:\ IISSiteBindings.txt

APPCMD列表網站> C:\ IISSiteBindings.txt

現在從命令提示符記事本打開。

記事本C:\ IISSiteBindings.txt

+0

不是PS答案;但又短又甜,還列出了可用於掃描IIS日誌的ID號。 – 2016-11-30 14:22:59

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 
    } 
} 
9

最簡單的方法,因爲我看到:

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

我發現這個問題,因爲我想生成鏈接到網頁所有在我的IIS上運行的網站。我使用Alexander Shapkin的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> 

我真的不知道Powershell,所以隨時編輯我的答案來清理它。

+0

我想我可能會在腳本中包含HTML,並放棄粘貼。 – 2018-02-13 20:16:48