【发布时间】:2020-01-06 10:43:11
【问题描述】:
我正在使用 WebAdministration 模块创建一个 Powershell 脚本来创建几个站点并自动添加绑定
Import-Module WebAdministration
$config = @{
Sites = @(
@{
Name = "Site1";
Path = "Path1";
Bindings = @(
@{ Protocol = "http"; Port = 80;},
@{ Protocol = "https"; Port = 443;}
);
},
@{
Name = "Site2";
Path = "Path2";
Bindings = @(
@{ Protocol = "http"; Port = 3009;}
);
}
)
}
foreach($site in $config.Sites){
$physicalPath = Get-Item "$($site.Path)"
# Create the current site
New-WebSite -Name "$($site.Name)" -PhysicalPath $physicalPath.FullName
## Trying to remove default port 80 binding for the current site
Remove-WebBinding -Name $site.Name -Port 80 -Protocol "http"
## Add the desired bindings
foreach ($binding in $site.Bindings){
New-WebBinding -Name "$($site.Name)" -Protocol $binding.Protocol -Port $binding.Port
}
}
但是,当我这样做时,我在端口 80 Site1 上没有绑定。看起来Remove-WebBinding -Name $site.Name -Port 80 -Protocol "http" 正在删除两个站点的绑定。
PS > Get-ChildItem IIS:\Sites
Name ID State Physical Path Bindings
---- -- ----- ------------- --------
Site1 1 Started Path1 https *:443: sslFlags=0
Site2 2 Stopped Path2 http *:3009:
如果我这样做而不尝试修改任何绑定
foreach($site in $config.Sites){
$physicalPath = Get-Item "$($site.Path)"
# Create the current site
New-WebSite -Name "$($site.Name)" -PhysicalPath $physicalPath.FullName
}
我最终将两个站点都绑定到端口 80
PS > Get-ChildItem IIS:\Sites
Name ID State Physical Path Bindings
---- -- ----- ------------- --------
Site1 1 Started Path1 http *:80:
Site2 2 Stopped Path2 http *:80:
我做错了什么?有没有更好的办法?这是一个错误吗?
【问题讨论】:
标签: powershell iis-10