简体   繁体   English

使用PowerShell将绑定添加到IIS,而不会覆盖现有目录

[英]Add Binding To IIS With PowerShell Without Overwriting Existing

We use OctopusDeploy to create the website and we use the create website template that is on the community step sites. 我们使用OctopusDeploy创建网站,并使用社区步骤站点上的create website模板。 I have modified it slightly to ensure that if the website exists it adds the default binding however it is removing all the existing bindings. 我对其进行了少许修改,以确保如果网站存在,它将添加默认绑定,但是它将删除所有现有绑定。 Is there a way to simply add a binding to IIS which overwrites if it already exists (or ignores) and doesn't remove all the existing bindings? 有没有一种方法可以简单地向IIS添加绑定,如果该绑定已经存在(或忽略)并且不删除所有现有绑定,则该覆盖会被覆盖?

The script we currently have is as follows: 我们当前拥有的脚本如下:

$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])
}

It seems this line: 看来这行:

        Set-ItemProperty -Path $sitePath -Name Bindings -Value ($wsBindings[0])

Is overwriting all existing bindings but I can't seem to find a way around it. 正在覆盖所有现有绑定,但是我似乎找不到解决方法。

You can use the New-WebBinding cmdlet: 您可以使用New-WebBinding cmdlet:

  New-WebBinding `
        -Name $webSiteName `
        -Protocol 'http' `
        -Port $bindingPort `
        -IPAddress $bindingIpAddress `
        -HostHeader $bindingHost

And use Get-WebBinding cmdlet to check whether the binding already exists. 并使用Get-WebBinding cmdlet检查绑定是否已经存在。

You can use New-WebBinding as jisaak said, that's mainly for http(s). 您可以使用jisaak所说的New-WebBinding ,主要用于http(s)。

If you need to add other types of bindings, you have to use New-ItemProperty , instead of Set-ItemProperty . 如果需要添加其他类型的绑定,则必须使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM