Loop through all bindings configured in IIS with powershell

后端 未结 3 1602
长情又很酷
长情又很酷 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.

提交回复
热议问题