« Previous Next »

Thread: Remove Host Header - unless it is the last one

Last post 09-24-2009 3:33 PM by rkr31. 3 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (4 items)

Sort Posts:

  • 09-16-2009, 6:31 AM

    • rkr31
    • Top 200 Contributor
    • Joined on 05-01-2007, 10:15 AM
    • Posts 30

    Remove Host Header - unless it is the last one

    Hi

     Im setting up a shared server using http 1.1 and need to automate the adding/removing of host headers.  The below script seems to work, however i need to add some logic to it so that if this is the last host header it will not remove it and will infact throw up an error of some kind that i can pass back to my web service.

     Remove-WebBinding -Site "Default Web Site" -Port 80 -IPAddress 127.0.0.1 -HostHeader www.site.com


    Any ideas?

    Thanks


    Richard

  • 09-17-2009, 5:39 PM In reply to

    • JeongHwan
    • Top 150 Contributor
    • Joined on 12-14-2004, 4:43 PM
    • Redmond, WA
    • Posts 33

    Re: Remove Host Header - unless it is the last one

    I am not clear about your scenario but I guess, you are trying to remove a certain binding but if the target site has only that binding you want to get exception instead of removing the binding. Am I right? If so, I think you can use this script code for that purpose.

        if ($null -ne (Get-WebBinding -name "Default Web Site" -port 80 -HostHeader www.site.com -IPAddress 127.0.0.1))
        {
            if ((Get-WebBinding -name "Default Web Site").count -gt 1) {
                Remove-WebBinding -name "Default Web Site" -port 80 -HostHeader www.site.com -IPAddress 127.0.0.1
            }
            else
            {
                throw "There is only one binding left for this site"
            }
        }
        else
        {
            throw "This site does not have the binding"
        }

    FYI, the parameter "Site" was renamed to "Name" in the latest build. So I used "Name" parameter instead of "Site".


    Jeong Hwan Kim
  • 09-18-2009, 5:40 AM In reply to

    • rkr31
    • Top 200 Contributor
    • Joined on 05-01-2007, 10:15 AM
    • Posts 30

    Re: Remove Host Header - unless it is the last one

    Thats excelent.  Exactly what i was looking for.  Thanks.

     

    Oh...before i forget.  Can we easily pass variables to the script so that i can just run

    test.ps1 www.site.com or similar.

    im sure i can google the answer, but if it's only a 2 minute job for you i would appreciate it ;o)

  • 09-24-2009, 3:33 PM In reply to

    • rkr31
    • Top 200 Contributor
    • Joined on 05-01-2007, 10:15 AM
    • Posts 30

    Re: Remove Host Header - unless it is the last one

    param([string]$headername) at the begining of the script and replacing the hostname with $headername resolved this :o)

Page 1 of 1 (4 items)
Microsoft Communities