[英]IIS: Display all sites and bindings in PowerShell
我正在記錄所有與 IIS 站點相關的站點和綁定。 是否有一種簡單的方法可以通過 PowerShell 腳本而不是手動輸入查看 IIS 來獲取此列表?
我希望輸出是這樣的:
Site Bindings
TestSite www.hello.com
www.test.com
JonDoeSite www.johndoe.site
試試這個:
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 語句的管道不會給你你需要的東西。 根據您的要求,我將構建一個自定義對象或哈希表。
嘗試這樣的事情來獲得你想要的格式:
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
我看到的最簡單的方法:
Foreach ($Site in get-website) { Foreach ($Bind in $Site.bindings.collection) {[pscustomobject]@{name=$Site.name;Protocol=$Bind.Protocol;Bindings=$Bind.BindingInformation}}}
如果您只想列出所有站點(即查找綁定)
將工作目錄更改為“C:\\Windows\\system32\\inetsrv”
cd c:\\Windows\\system32\\inetsrv
接下來運行“appcmd list sites”(復數)並輸出到文件。 例如 c:\\IISSiteBindings.txt
appcmd 列出站點 > c:\\IISSiteBindings.txt
現在從命令提示符用記事本打開。
記事本 c:\\IISSiteBindings.txt
試試這個
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
}
}
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
}
}
我找到這個頁面是因為我需要將一個具有許多綁定的站點遷移到新服務器。 我在這里使用了一些代碼來生成下面的 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
我發現這個問題是因為我想生成一個網頁,其中包含指向在我的 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.