使用PowerShell循环遍历IIS中配置的所有绑定 [英] Loop through all bindings configured in IIS with powershell

查看:336
本文介绍了使用PowerShell循环遍历IIS中配置的所有绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来浏览我在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

我的数组$网站已正确填写并且以下命令......

My array $Websites is filled correctly and with the following command...

$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 only need the binding. I tried:

foreach ($site in $Websites)
{
    $site = $Websites[0]
    $site | select-string "http"
}

调试该代码向我显示$ Site doesn'包含我的预期: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 to pipeline和filtere only object,其中property 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中配置的所有绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆