[英]powershell script to apply host header name to an existing binding of a site in IIS
[英]Add Binding To IIS With PowerShell Without Overwriting Existing
我們使用OctopusDeploy創建網站,並使用社區步驟站點上的create website模板。 我對其進行了少許修改,以確保如果網站存在,它將添加默認綁定,但是它將刪除所有現有綁定。 有沒有一種方法可以簡單地向IIS添加綁定,如果該綁定已經存在(或忽略)並且不刪除所有現有綁定,則該覆蓋會被覆蓋?
我們當前擁有的腳本如下:
$bindingInformation = "${bindingIpAddress}:${bindingPort}:${bindingHost}"
$sitePath = ("IIS:\Sites\" + $webSiteName)
$site = Get-Item $sitePath -ErrorAction SilentlyContinue
if (!$site) {
Write-Output "Creating web site $webSiteName"
$id = (dir iis:\sites | foreach {$_.id} | sort -Descending | select -first 1) + 1
new-item $sitePath -bindings ($wsBindings[0]) -id $id -physicalPath $webRoot -confirm:$false
} else {
write-host "Web site $webSiteName already exists"
Set-ItemProperty -Path $sitePath -Name Bindings -Value ($wsBindings[0])
}
看來這行:
Set-ItemProperty -Path $sitePath -Name Bindings -Value ($wsBindings[0])
正在覆蓋所有現有綁定,但是我似乎找不到解決方法。
您可以使用New-WebBinding cmdlet:
New-WebBinding `
-Name $webSiteName `
-Protocol 'http' `
-Port $bindingPort `
-IPAddress $bindingIpAddress `
-HostHeader $bindingHost
並使用Get-WebBinding cmdlet檢查綁定是否已經存在。
您可以使用jisaak所說的New-WebBinding
,主要用於http(s)。
如果需要添加其他類型的綁定,則必須使用New-ItemProperty
而不是Set-ItemProperty
。
New-ItemProperty -path "IIS:\Sites\YourSiteName" `
-name bindings -value @{protocol="net.pipe";bindingInformation="SiteName"}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.