问题描述
我正在寻找一种方法来检查已在我的 IIS 中配置的所有绑定设置.
I'm looking for a way to go through all binding settings already configured in my IIS.
我正在使用它来处理 Powershell 中的 IIS:
Im using this to work with the IIS in Powershell:
Import-Module WebAdministration
到目前为止,我能够获得所需的主要信息:
So far I was able to get the main required information i want:
$Websites = Get-ChildItem IIS:Sites
我的数组 $Websites 已正确填充并使用以下命令...
My array $Websites is filled correctly and with the following command...
$Websites[2]
..我收到这个结果:
Name ID State Physical Path Bindings
---- -- ----- ------------- --------------
WebPage3 5 D:WebPage3 http *:80:WebPage3
https *:443:WebPage3
现在是我遇到困难的部分:
Now here's the part I having a hard time with:
我想检查绑定是否正确.为了做到这一点,我只需要绑定.我试过了:
I want to check if the binding is correct. In order to do that I only need the binding. I tried:
foreach ($site in $Websites)
{
$site = $Websites[0]
$site | select-string "http"
}
调试该代码显示 $Site 不包含我预期的内容:Microsoft.IIs.PowerShell.Framework.ConfigurationElement".我目前不知道如何显式获取绑定信息以获取类似这样的信息(在 foreach 循环内):
Debugging that code shows me that $Site doesn't contain what I expected: "Microsoft.IIs.PowerShell.Framework.ConfigurationElement". I currently have no clue how to explicitly get to the binding information in order to to something like this (inside the foreach loop):
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
}
}
推荐答案
我不知道你到底想做什么,但我会试试.我看到您引用了 $Websites[2]
,即 webPage3.你可以这样做:
I don't know exactly what you are trying to do, but I will try. I see that you reference $Websites[2]
which is webPage3.You can do it like this:
$site = $websites | Where-object { $_.Name -eq 'WebPage3' }
那么当你查看$site.Bindings
时,你会意识到你需要Collection
成员:
Then when you look at $site.Bindings
, you will realize that you need the Collection
member:
$site.bindings.Collection
在我的机器上返回这个:
On my machine this returns this:
protocol bindingInformation
-------- ------------------
http *:80:
net.tcp 808:*
net.pipe *
net.msmq localhost
msmq.formatname localhost
https *:443:
然后测试可能如下所示:
And the test might then look like this:
$is80 = [bool]($site.bindings.Collection | ? { $_.bindingInformation -eq '*:80:' })
if ($is80) {
#website is ok
} else {
#remove all current binding
#add correct binding
}
我将 Collection
的内容发送到管道并仅过滤属性 bindingInformation
等于所需值的对象(更改它).然后我将其转换为 [bool]
.如果有需要的项目,这将返回 $true
,否则返回 $false
.
I sent content of Collection
to pipeline and filtere only objects where property bindingInformation
is equal to desired value (change it). Then I cast it to [bool]
. This will return $true
if there is desired item, $false
otherwise.
这篇关于使用 powershell 循环遍历 IIS 中配置的所有绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!