Format Issue (those curly brackets!!) Listing IIS sites (IP and protocol)

Welcome Forums General PowerShell Q&A Format Issue (those curly brackets!!) Listing IIS sites (IP and protocol)

Viewing 4 reply threads
  • Author
    Posts
    • #227383
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

      Hi Folks

      I have a small PS script that will list all websites, and the IP it is bound too, and if it has a http or https binding.

      So, it should look like this:

      Name                IP            Protocol     
      ----                --------      --------     
      Default Web Site    *             http         
      site.contoso.com    192.168.1.1   http, https

      But instead it’s showing as

      Name                IP            Protocol     
      ----                --------      --------     
      Default Web Site    *             http         
      site.contoso.com    192.168.1.1   {http, https}

      My code is:

      $data = @()
      foreach ($site in get-childitem IIS:\Sites\)
       {
          $item = @{}
          $item.Name = $site.name
          $item.IP = ((Get-WebBinding -Name $site.name).bindingInformation).Split(':')[0]
          $item.Protocol = (Get-WebBinding -Name $site.name).protocol | Sort-Object -Unique
          $data += New-Object PSObject -Property $item
       }
       $data = $data | Select-Object Name,IP,Protocol | Sort-Object IP

      I am splitting the bindings to get the first part of the bindings (i.e. the IP address) Nb. Our sites can have mulitple host headers but only 1 IP, so that’s the reason for the split – to just get first IP.

      But for the protocol, some sites are bound to IP and have a https binding as well, and others only a dedicated IP. So, to not show http, http, http, https – I tried to sort-object -Unique to just get one of each http and/or https etc.

      But I can’t get it show the protocol without the curly brackets! 🙁

      I know that they are added because it’s not just a string, but array of values…. but I thought there must be an easy way to display that without those curly suckers?!

      Thanks

      Alex

       

       

       

    • #227395
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      I think you’ll just need a line per protocol if you don’t want it to show the curly braces. As you said, it’s how powershell displays a nested group.

      @{
          one = @{
              a = 'a'
              b = 'b'
          }
          two = 'c'
      }
      
      Name                           Value                                                                                                             
      ----                           -----                                                                                                             
      one                            {a, b}                                                                                                            
      two                            c     
      
    • #227398
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      But I can’t get it show the protocol without the curly brackets!

      That’s Powershells way to display an array. If you want to output a comma separated list of the array elements you can do it like this:

      $data = 
      foreach ($site in get-childitem IIS:\Sites\) {
          [PSCustomObject]@{
              Name     = $site.name
              IP       = ((Get-WebBinding -Name $site.name).bindingInformation).Split(':')[0]
              Protocol = @((Get-WebBinding -Name $site.name).protocol | Sort-Object -Unique) -join ','
          }
      }
      $data | Sort-Object IP
    • #227458
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

      Thanks Doug!

      Hey Olaf! Thanks…(again) ! You helped me a couple years back with PS/IIS stuff too! Always a pleasure to see your avitar help out on my questions!

    • #227467
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      Nicely done Olaf! It could also join with spaces but unfortunately “n" "r” did not put it on additional lines.

      @{
          one = @{
              a = 'a'
              b = 'b'
              c = 'c'
              d = 'd'
          } | foreach {$_.values -join ","}
          two = 'c'
      }
      
      Name                           Value                                                                                                                            
      ----                           -----                                                                                                                            
      one                            c,d,b,a                                                                                                                          
      two                            c 
      

      I learned something as well, thanks again.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.