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

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! :frowning:

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

 

 

 

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     

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

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!

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.