windows 使用 powershell 遍历 IIS 中配置的所有绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6186888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop through all bindings configured in IIS with powershell
提问by FullByte
I'm looking for a way to go through all binding settingsalready configured in my IIS.
我正在寻找一种方法来完成已在我的 IIS 中配置的所有绑定设置。
Im using this to work with the IIS in Powershell:
我使用它来处理 Powershell 中的 IIS:
Import-Module WebAdministration
So far I was able to get the main required information i want:
到目前为止,我能够获得所需的主要信息:
$Websites = Get-ChildItem IIS:\Sites
My array $Websites is filled correctly and with the following command...
我的数组 $Websites 已正确填充并使用以下命令...
$Websites[2]
..I recieve this result:
..我收到这个结果:
Name ID State Physical Path Bindings
---- -- ----- ------------- --------------
WebPage3 5 D:\Web\Page3 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 onlyneed the binding. I tried:
我想检查绑定是否正确。为了做到这一点,我只需要绑定。我试过:
foreach ($site in $Websites)
{
$site = $Websites[0]
$site | select-string "http"
}
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):
调试该代码显示 $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
}
Thank you for your help!
感谢您的帮助!
Solution:
解决方案:
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
}
}
采纳答案by stej
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:
我不知道你到底想做什么,但我会努力的。我看到您引用的$Websites[2]
是webPage3。你可以这样做:
$site = $websites | Where-object { $_.Name -eq 'WebPage3' }
Then when you look at $site.Bindings
, you will realize that you need the Collection
member:
然后当你查看 时$site.Bindings
,你会意识到你需要这个Collection
成员:
$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
}
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.
我将内容发送Collection
到管道并仅过滤属性bindingInformation
等于所需值的对象(更改它)。然后我把它投射到[bool]
. $true
如果有所需的项目,这将返回,$false
否则将返回。
回答by user3794864
I found that if there were multiple bindings on a site then if I needed to script access to individual parts of the bindings otherwise I only got the first binding. To get them all I needed the script to be extended as below:
我发现如果一个站点上有多个绑定,那么如果我需要对绑定的各个部分进行脚本访问,否则我只会得到第一个绑定。为了得到它们,我需要将脚本扩展如下:
Import-Module WebAdministration
$Websites = Get-ChildItem IIS:\Sites
foreach ($Site in $Websites) {
$Binding = $Site.bindings
[string]$BindingInfo = $Binding.Collection
[string[]]$Bindings = $BindingInfo.Split(" ")
$i = 0
$header = ""
Do{
Write-Output ("Site :- " + $Site.name + " <" + $Site.id +">")
Write-Output ("Protocol:- " + $Bindings[($i)])
[string[]]$Bindings2 = $Bindings[($i+1)].Split(":")
Write-Output ("IP :- " + $Bindings2[0])
Write-Output ("Port :- " + $Bindings2[1])
Write-Output ("Header :- " + $Bindings2[2])
$i=$i+2
} while ($i -lt ($bindings.count))
}
回答by Omid
I had something similar to the last answer, but this corrects to HTTPS sites and adds a bit more information that is useful.
我有一些类似于上一个答案的内容,但这更正了 HTTPS 站点并添加了更多有用的信息。
Import-Module WebAdministration
$hostname = hostname
$Websites = Get-ChildItem IIS:\Sites
$date = (Get-Date).ToString('MMddyyyy')
foreach ($Site in $Websites) {
$Binding = $Site.bindings
[string]$BindingInfo = $Binding.Collection
[string[]]$Bindings = $BindingInfo.Split(" ")#[0]
$i = 0
$status = $site.state
$path = $site.PhysicalPath
$fullName = $site.name
$state = ($site.name -split "-")[0]
$Collection = ($site.name -split "-")[1]
$status = $site.State
$anon = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/AnonymousAuthentication -Name Enabled -PSPath IIS:\sites -Location $site.name | select-object Value
$basic = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/BasicAuthentication -Name Enabled -PSPath IIS:\ -location $site.name | select-object Value
Do{
if( $Bindings[($i)] -notlike "sslFlags=*"){
[string[]]$Bindings2 = $Bindings[($i+1)].Split(":")
$obj = New-Object PSObject
$obj | Add-Member Date $Date
$obj | Add-Member Host $hostname
$obj | Add-Member State $state
$obj | Add-Member Collection $Collection
$obj | Add-Member SiteName $Site.name
$obj | Add-Member SiteID $site.id
$obj | Add-member Path $site.physicalPath
$obj | Add-Member Protocol $Bindings[($i)]
$obj | Add-Member Port $Bindings2[1]
$obj | Add-Member Header $Bindings2[2]
$obj | Add-member AuthAnon $Anon.value
$obj | Add-member AuthBasic $basic.value
$obj | Add-member Status $status
$obj #take this out if you want to save to csv| export-csv "c:\temp$date-$hostname.csv" -Append -notypeinformation
$i=$i+2
}
else{$i=$i+1}
} while ($i -lt ($bindings.count))
}