Why Powershell's New-WebBinding commandlet creates incorrect HostHeader?
Asked Answered
E

3

12

I am trying to add an MSMQ binding for my IIS Web Site, correct binding should look like this:

enter image description here

So I am executing following line in PowerShell:

New-WebBinding -Name "My Site"  -Protocol net.msmq -HostHeader "localhost"

and it creates the following binding:

enter image description here

prefixing it with *:80:, so my MSMQ messages don't get picked up by WCF service. Maybe I am doing it wrong? How to create a binding with Binding Information set to just "localhost" using this PowerShell comandlet?

Commandlet codumentaiton can be found here.

Enthrall answered 24/2, 2012 at 2:3 Comment(0)
A
8

Looking at the decompiled code of the cmdlet, looks like it adding the IPAddress and Port information in the binding and there is no workaround to it.

Relevant sections from the code:

private string ipAddress = "*";
...
builder.Append(this.ipAddress);
...
builder.Append(":" + this.sitePort.ToString(CultureInfo.InvariantCulture) + ":");

But you can do what the cmdlet actually does ( below code from cmdlet):

new-itemproperty -path "IIS:\sites\test" -name bindings -value @{protocol="net.msmq"; bindingInformation="localhost"}
Aldwon answered 24/2, 2012 at 4:14 Comment(8)
Oh, could you please tell me how to decompile cmdlets? Thanks.Enthrall
@Enthrall Checkout ILSpy and dotPeek.Tragopan
I know about them, but what assembly do I need to decombile?Enthrall
@Enthrall Depends, but most are in Microsoft.PowerShell.Commands. Browse around in System.Management.Automation.Tragopan
@Enthrall - look at $pshome\Modules\WebAdministration\Microsoft.IIS.PowerShell.Provider.dllAldwon
@Enthrall For snap-ins you can do: get-pssnapin -Name WebAdministration | select *. Look at ModuleName. Also check out the rest: get-pssnapin | Select ModuleName.Tragopan
@Enthrall Another way is: (get-command $cmdletName).DLL e.g. (get-command New-WebBinding).DLL.Tragopan
I keep getting the following error when doing the new-itemproperty way: New-ItemProperty : A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'. This appears to be on the -value parameter I think. Anyone get similar problems?Unchaste
T
2

Give this a try:

New-ItemProperty "IIS:\sites\NameOfYourSite" -name bindings -value @{protocol="net.msmq";bindingInformation="localhost"}
Tragopan answered 24/2, 2012 at 3:57 Comment(2)
I keep getting the following error when doing the new-itemproperty way: New-ItemProperty : A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'. This appears to be on the -value parameter I think. Any suggestions?Unchaste
@Acquire hmm should work, did you import the webadministration module?Tragopan
C
0

If your are running PowerShell (Core), a.k.a PowerShell >v7.1.x, you will find yourself in trouble because...

WARNING: Module WebAdministration is loaded in Windows PowerShell using WinPSCompatSession remoting session;
please note that all input and output of commands from this module will be deserialized objects.
If you want to load this module into PowerShell please use 'Import-Module -SkipEditionCheck' syntax.

The IIS provider isn't available via remoting session.

The easiest trick is to redirect string via pipeline to Windows PowerShell.

"Import-Module WebAdministration;New-ItemProperty -Path `"IIS:\Sites\$($configuration.Website.Name)`" -Name Bindings -value @{protocol = `"net.msmq`"; bindingInformation = `"localhost`" }" | PowerShell

In this example, the website name is read from the configuration JSON. You can replace it by a hard-coded site name.

Clarinda answered 31/12, 2020 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.