Loop through all bindings configured in IIS with powershell

后端 未结 3 1571
长情又很酷
长情又很酷 2021-01-05 09:23

I\'m looking for a way to go through all binding settings already configured in my IIS.

Im using this to work with the IIS in Powershell:

Im         


        
相关标签:
3条回答
  • 2021-01-05 09:38

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

    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
     }
    

    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.

    0 讨论(0)
  • 2021-01-05 09:56

    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))
    }
    
    0 讨论(0)
  • 2021-01-05 10:00

    I had something similar to the last answer, but this corrects to HTTPS sites and adds a bit more information that is useful.

    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))
    }
    
    0 讨论(0)
提交回复
热议问题