使用多个绑定从Powershell在IIS中创建网站


8

我试图做简单的脚本,所以我的网站可以既作为达到www.example.com和简单example.com。如何将其作为绑定参数传递?

这是我尝试的方法:

$iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation="*:80:"+ $url + ",*:80:www." + $url} -physicalPath $directoryPath
$iisApp | Set-ItemProperty -Name "applicationPool" -Value $iisAppPoolName

Answers:


7

bindingInformation选项需要一个条目数组(每个数组本身,请注意@),而不是用逗号分隔的列表。

示例-首先定义一个适当的条目数组,然后将其分配为bindingInformation参数:

$bindings = @(
   @{protocol="http";bindingInformation="*:80:" + $url},
   @{protocol="http";bindingInformation="*:80:www." + $url},
)

$iisApp = New-Item $iisAppName -bindings $bindings -physicalPath $directoryPath
$iisApp | Set-ItemProperty -Name "applicationPool" -Value $iisAppPoolName

或者,在创建站点之后,您可以使用New-WebBinding命令添加其他绑定。例如:

New-WebBinding -Name $iisAppName -IPAddress "*" -Port 80 -HostHeader "www.$url"

0

我用它来更新绑定可能会帮助某人,因为我必须自己弄清楚这一点。

$hostname =$env:COMPUTERNAME
$fqdn = $env:USERDNSDOMAIN
$Bindings = Get-WebBinding |Select -expandproperty bindinginformation
$websites = Get-Website
foreach ($website in $websites)
    {
    $siteName=$website.name
         foreach ($Binding in $Bindings)
                {
                $oldheader =($Binding -split ":")[-1]
                    if ($oldheader -eq "")
                        {
                         Set-WebBinding -Name $sitename -BindingInformation $Binding -PropertyName "HostHeader" -Value "$hostname.$fqdn" 
                        }
                }
     }

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.