Mike Christensen
Mike Christensen

Reputation: 91608

How to select binding with no host header using Get-WebBinding

I have the following bindings:

enter image description here

I can select the binding which has the host header api like so:

enter image description here

However, I cannot select the binding which has no host header. If I pass in "" or $NUL, I still get all bindings:

enter image description here

One possible solution I've found is to filter the list using Where-Object, like so:

enter image description here

Is there a better way to do this which will get me the default binding with no host header and nothing else? Thanks!

Upvotes: 3

Views: 1441

Answers (2)

GTEM
GTEM

Reputation: 3

I realize this is an old thread, but for the benefit of others that search and find this thread. Try this

(Get-WebBinding).BindingInformation | ? {$_ -like '*:'}

Upvotes: 0

HAL9256
HAL9256

Reputation: 13453

It really looks like you can't explicitly use Get-WebBinding to get just the one with no Host Header.

Get-WebBinding returns all bindings. From the docs, our only available parameters for filtering are:

  • -Name - We are already using to specify website name
  • -IPAddress - N/A in this case
  • -Port - can only really specify 443 which won't help
  • -Protocol - can only specify https which won't help
  • -HostHeader - our best hope - Let's investigate.

Our only option is to use -HostHeader. We know it accepts a type string, so we can try all tools in our toolbox:

Get-WebBinding -Name "Default Web Site" -HostHeader ""
Get-WebBinding -Name "Default Web Site" -HostHeader $null
Get-WebBinding -Name "Default Web Site" -HostHeader ([String]::Empty)
Get-WebBinding -Name "Default Web Site" -HostHeader "*"

They return all the same entries:

protocol bindingInformation sslFlags
-------- ------------------ --------
https    *:443:api          1
https    *:443:api.cluster  1
https    *:443:             0

i.e. all of them. This makes sense because the -HostHeader parameter is a wildcard Filter. We can only filter entries and not select entries. An "Empty" filter (i.e. "", $null, [String]::Empty) is the same as returning everything (i.e. that's why it returned the same as the full wildcard "*").

We can prove this by changing the filter to:

Get-WebBinding -Name "Default Web Site" -HostHeader "api*"

Which returns:

protocol bindingInformation sslFlags
-------- ------------------ --------
https    *:443:api          1
https    *:443:api.cluster  1

The exact opposite of what we want.

Since Get-WebBinding only has filtering parameters, we need to combine it with a selection parameter like Where-Object. Since sslflags is the flag for "Require Server Name Indication", the "better" way is to select based on the bindingInformation:

Get-WebBinding -Name "Default Web Site" | Where-Object { $_.bindingInformation -eq '*:443:' }

Returning what we want:

protocol bindingInformation sslFlags
-------- ------------------ --------
https    *:443:             0

Upvotes: 4

Related Questions