【发布时间】:2011-05-31 11:29:39
【问题描述】:
我正在寻找一种方法来检查已在我的 IIS 中配置的所有绑定设置。
我正在使用它来处理 Powershell 中的 IIS:
Import-Module WebAdministration
到目前为止,我能够获得所需的主要信息:
$Websites = Get-ChildItem IIS:\Sites
我的数组 $Websites 已正确填充并使用以下命令...
$Websites[2]
..我收到这个结果:
Name ID State Physical Path Bindings
---- -- ----- ------------- --------------
WebPage3 5 D:\Web\Page3 http *:80:WebPage3
https *:443:WebPage3
现在是我遇到困难的部分:
我想检查绑定是否正确。为了做到这一点,我只需要绑定。我试过了:
foreach ($site in $Websites)
{
$site = $Websites[0]
$site | select-string "http"
}
调试该代码显示 $Site 不包含我预期的内容:“Microsoft.IIs.PowerShell.Framework.ConfigurationElement”。我目前不知道如何显式获取绑定信息以获取类似这样的信息(在 foreach 循环内):
if ($site.name -eq "WebPage3" -and $site.Port -eq "80") {
#website is ok
}
else {
#remove all current binding
#add correct binding
}
感谢您的帮助!
解决方案:
Import-Module WebAdministration
$Websites = Get-ChildItem IIS:\Sites
foreach ($Site in $Websites) {
$Binding = $Site.bindings
[string]$BindingInfo = $Binding.Collection
[string]$IP = $BindingInfo.SubString($BindingInfo.IndexOf(" "),$BindingInfo.IndexOf(":")-$BindingInfo.IndexOf(" "))
[string]$Port = $BindingInfo.SubString($BindingInfo.IndexOf(":")+1,$BindingInfo.LastIndexOf(":")-$BindingInfo.IndexOf(":")-1)
Write-Host "Binding info for" $Site.name " - IP:"$IP", Port:"$Port
if ($Site.enabledProtocols -eq "http") {
#DO CHECKS HERE
}
elseif($site.enabledProtocols -eq "https") {
#DO CHECKS HERE
}
}
【问题讨论】:
-
你试过
get-webbinding、remove-webbinding和new-webbinding吗? -
@empo:get-webbinding 工作正常(谢谢!)
-
欢迎。我删除了我的答案,因为我收到了反对票(那里可能有问题),但您可以使用标准 cmdlet 正常工作。 remove-webbinding 和 new-webbinding 也可以正常工作。
-
如果你只是想查看或列出所有绑定试试这个:stackoverflow.com/a/35158534/4502867
标签: windows powershell iis-7