繁体   English   中英

使用 powershell 循环遍历 IIS 中配置的所有绑定

[英]Loop through all bindings configured in IIS with powershell

我正在通过我的 IIS 中已配置的所有绑定设置寻找 go 的方法。

我使用它与 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
    }
}

我不知道你到底想做什么,但我会试试的。 我看到您引用$Websites[2] ,即webPage3 你可以这样做:

$site = $websites | Where-object { $_.Name -eq 'WebPage3' }

然后当您查看$site.Bindings时,您会意识到您需要Collection成员:

$site.bindings.Collection

在我的机器上,这会返回:

protocol                       bindingInformation
--------                       ------------------
http                           *:80:
net.tcp                        808:*
net.pipe                       *
net.msmq                       localhost
msmq.formatname                localhost
https                          *:443:

然后测试可能如下所示:

$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

我发现如果一个站点上有多个绑定,那么如果我需要编写脚本来访问绑定的各个部分,否则我只会得到第一个绑定。 为了得到它们,我需要将脚本扩展如下:

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))
}

我有类似于上一个答案的内容,但这更正了 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))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM