1

I have a single host with 500+ bindings associated to it, and I would like to first purge this host of all its bindings and replace them from a list of bindings in a file. The format of the fill will be a comma separated list of the bindings to assign i.e. site.com,site1.com, etc.

so far I have stared down of first finding the host I want to perform this task on, that trying to remove all the bindings....basically I am trying to:: 1: remove all bindings currently assigned, 2: add bindings from a files.

Update 1: I now have each binding I want to be deleted writing to a file....now how can I get this to delete the actual binding...

Update 2: After pulling my hair out for way too long, I finally have the bindings getting purged and a new binding added to the selected domain. All I have to do know is hook up an insert look reading in a file for a list of bindings to add....

OPTION EXPLICIT
DIM CRLF, TAB, strServer, objWebService, domains, oIIS, oBindingNew, oSite
TAB  = CHR( 9 )
CRLF = CHR( 13 ) & CHR( 10 )
IF WScript.Arguments.Length = 1 THEN
    strServer = WScript.Arguments( 0 )
ELSE
    strServer = "localhost"
END IF
SET objWebService = GetObject( "IIS://" & strServer & "/W3SVC" )
EnumWebsites objWebService
SUB EnumWebsites( objWebService )
    DIM objWebServer, objWebServerRoot, strBindings

    FOR EACH objWebServer IN objWebService

            IF objWebserver.Class = "IIsWebServer" THEN
            IF objWebserver.ServerComment = "MobileCC" THEN
            SET objWebServerRoot = GetObject(objWebServer.adspath & "/root")
                    WScript.Echo _
                            "Site ID = " & objWebserver.Name & CRLF & _
                            "Comment = """ & objWebServer.ServerComment & """ " & CRLF & _
                            "State   = " & State2Desc( objWebserver.ServerState ) & CRLF & _
                            "Path   = " & objWebServerRoot.path & CRLF & _
                            "LogDir  = " & objWebServer.LogFileDirectory & _
                            ""
                    ' Enumerate the HTTP bindings (ServerBindings) and
                    ' SSL bindings (SecureBindings)
                    strBindings = EnumBindings( objWebServer.ServerBindings ) & _
                                                EnumBindings( objWebServer.SecureBindings )
                    IF NOT strBindings = "" THEN
                            ' Output current bindings
                            WScript.Echo "IP Address" & TAB & _
                                                     "Port" & TAB & _
                                                     "Host" & CRLF & _
                                                     strBindings
                    END IF

                    ' Reset Bindings for this domain
                    objWebserver.Put "ServerBindings", ""
            objWebserver.SetInfo

                    ' add a new binding
                    domains="www.NEWBINDING.com"
                    Set oIIS = GetObject("winmgmts:root\WebAdministration")
                    Set oBindingNew = oIIS.Get("BindingElement").SpawnInstance_            
                    oBindingNew.BindingInformation = "*:80:" & domains            
                    oBindingNew.Protocol = "http"  
                    Set oSite = oIIS.Get("Site.Name='MobileCC'")
                    oSite.Bindings= array(oBindingNew)
                    oSite.put_


            END IF
            END IF                  
    NEXT
        FOR EACH objWebServer IN objWebService
            IF objWebserver.Class = "IIsWebServer" THEN
            IF objWebserver.ServerComment = "MobileCC" THEN
                    ' Enumerate the HTTP bindings (ServerBindings) and
                    ' SSL bindings (SecureBindings)
                    strBindings = EnumBindings( objWebServer.ServerBindings ) & _
                                                EnumBindings( objWebServer.SecureBindings )
                    IF NOT strBindings = "" THEN
                            ' Output current bindings
                            WScript.Echo "IP Address" & TAB & _
                                                     "Port" & TAB & _
                                                     "Host" & CRLF & _
                                                     strBindings
                    END IF
            END IF
            END IF
        NEXT    
END SUB

FUNCTION EnumBindings( objBindingList )
    DIM i, strIP, strPort, strHost
    DIM reBinding, reMatch, reMatches
    SET reBinding = NEW RegExp
    reBinding.Pattern = "([^:]*):([^:]*):(.*)"

    FOR i = LBOUND( objBindingList ) TO UBOUND( objBindingList )
        ' objBindingList( i ) is a string looking like IP:Port:Host
        SET reMatches = reBinding.Execute( objBindingList( i ) )
        FOR EACH reMatch IN reMatches
            strIP = reMatch.SubMatches( 0 )
            strPort = reMatch.SubMatches( 1 )
            strHost = reMatch.SubMatches( 2 )

            ' Do some pretty processing
            IF strIP = "" THEN strIP = "All Unassigned"
            IF strHost = "" THEN strHost = "*"
            IF LEN( strIP ) < 8 THEN strIP = strIP & TAB

            EnumBindings = EnumBindings & _
                           strIP & TAB & _
                           strPort & TAB & _
                           strHost & TAB & _
                           ""
        NEXT

        EnumBindings = EnumBindings & CRLF
    NEXT

END FUNCTION

FUNCTION State2Desc( nState )
    SELECT CASE nState
    CASE 1
        State2Desc = "Starting (MD_SERVER_STATE_STARTING)"
    CASE 2
        State2Desc = "Started (MD_SERVER_STATE_STARTED)"
    CASE 3
        State2Desc = "Stopping (MD_SERVER_STATE_STOPPING)"
    CASE 4
        State2Desc = "Stopped (MD_SERVER_STATE_STOPPED)"
    CASE 5
        State2Desc = "Pausing (MD_SERVER_STATE_PAUSING)"
    CASE 6
        State2Desc = "Paused (MD_SERVER_STATE_PAUSED)"
    CASE 7
        State2Desc = "Continuing (MD_SERVER_STATE_CONTINUING)"
    CASE ELSE
        State2Desc = "Unknown state"
    END SELECT

END FUNCTION
3
  • Describe what you're trying to do using an example of what you're trying to do at the bindings level. The comma-separated list and file access bits are stuff you can tack on later; do it with one first, then generalize and expand on that.
    – TristanK
    Dec 1, 2011 at 22:12
  • basically I am trying to:: 1: remove all bindings currently assigned, 2: add bindings from a files Dec 1, 2011 at 22:18
  • just updated the script "Update 1: I now have each binding I want to be deleted writing to a file....now how can I get this to delete the actual binding..." Dec 2, 2011 at 0:36

2 Answers 2

0

after tons of lonely research, I hope this answer truly helps others save time.

feel free to comment if you have any questions, it's not pretty and I am sure it could be written better but for a one of task it works.

OPTION EXPLICIT
DIM CRLF, TAB, strServer, objWebService, objWebServerTMP, objWebServer, objWebServerRoot, strBindings, domain, domains, domainsMobile, domainsMobileCC, oIIS, oBindingNew, oSite, ArrayOfValues, MobileSiteName
TAB  = CHR( 9 )
CRLF = CHR( 13 ) & CHR( 10 )
IF WScript.Arguments.Length = 1 THEN
    strServer = WScript.Arguments( 0 )
ELSE
    strServer = "localhost"
END IF
SET objWebService = GetObject( "IIS://" & strServer & "/W3SVC" )
EnumWebsites objWebService

SUB EnumWebsites( objWebService )

    FOR EACH objWebServer IN objWebService

            IF objWebserver.Class = "IIsWebServer" THEN
            '**************************************************
            IF objWebserver.ServerComment = "SiteName" THEN
            '**************************************************
            SET objWebServerRoot = GetObject(objWebServer.adspath & "/root")
                    WScript.Echo _
                            "Site ID = " & objWebserver.Name & CRLF & _
                            "Comment = """ & objWebServer.ServerComment & """ " & CRLF & _
                            "State   = " & State2Desc( objWebserver.ServerState ) & CRLF & _
                            "Path   = " & objWebServerRoot.path & CRLF & _
                            "LogDir  = " & objWebServer.LogFileDirectory & _
                            ""
                    ' Enumerate the HTTP bindings (ServerBindings) and
                    ' SSL bindings (SecureBindings)
                    strBindings = EnumBindings( objWebServer.ServerBindings ) & _
                                                EnumBindings( objWebServer.SecureBindings )
                    IF NOT strBindings = "" THEN
                            ' Output current bindings
                            WScript.Echo "IP Address" & TAB & _
                                                     "Port" & TAB & _
                                                     "Host" & CRLF & _
                                                     strBindings
                    END IF

                    ' Reset Bindings for this domain
                    objWebserver.Put "ServerBindings", ""
            objWebserver.SetInfo
                    '**************************************************
                    domainsM="www.test1.com,www.test2.com"
                    ArrayOfValues = Split(domainsM,",")
                    '**************************************************
                    'For i = 0 To Ubound(ArrayOfValues)
                    '       WScript.Echo ArrayOfValues(i)
                    'NEXT

                    Dim x
                    For x = 0 To Ubound(ArrayOfValues)

                        domain=ArrayOfValues(x)
                        Set oIIS = GetObject("winmgmts:root\WebAdministration")
                        Set oBindingNew = oIIS.Get("BindingElement").SpawnInstance_            
                        oBindingNew.BindingInformation = "*:80:" & domain            
                        oBindingNew.Protocol = "http"  
                        '**************************************************
                        Set oSite = oIIS.Get("Site.Name='SiteName'")
                        '**************************************************
                        oSite.Bindings= array(oBindingNew)
                        oSite.put_

                    NEXT

            END IF
            END IF                  
    NEXT
        FOR EACH objWebServerTMP IN objWebService
            IF objWebServerTMP.Class = "IIsWebServer" THEN
            '**************************************************
            IF objWebServerTMP.ServerComment = "SiteName" THEN
            '**************************************************
                    ' Enumerate the HTTP bindings (ServerBindings) and
                    ' SSL bindings (SecureBindings)
                    strBindings = EnumBindings( objWebServerTMP.ServerBindings ) & _
                                                EnumBindings( objWebServerTMP.SecureBindings )
                    IF NOT strBindings = "" THEN
                            ' Output current bindings
                            WScript.Echo "IP Address" & TAB & _
                                                     "Port" & TAB & _
                                                     "Host" & CRLF & _
                                                     strBindings
                    END IF
            END IF
            END IF
        NEXT    
END SUB

FUNCTION EnumBindings( objBindingList )
    DIM i, strIP, strPort, strHost
    DIM reBinding, reMatch, reMatches
    SET reBinding = NEW RegExp
    reBinding.Pattern = "([^:]*):([^:]*):(.*)"

    FOR i = LBOUND( objBindingList ) TO UBOUND( objBindingList )
        ' objBindingList( i ) is a string looking like IP:Port:Host
        SET reMatches = reBinding.Execute( objBindingList( i ) )
        FOR EACH reMatch IN reMatches
            strIP = reMatch.SubMatches( 0 )
            strPort = reMatch.SubMatches( 1 )
            strHost = reMatch.SubMatches( 2 )

            ' Do some pretty processing
            IF strIP = "" THEN strIP = "All Unassigned"
            IF strHost = "" THEN strHost = "*"
            IF LEN( strIP ) < 8 THEN strIP = strIP & TAB

            EnumBindings = EnumBindings & _
                           strIP & TAB & _
                           strPort & TAB & _
                           strHost & TAB & _
                           ""
        NEXT

        EnumBindings = EnumBindings & CRLF
    NEXT

END FUNCTION

FUNCTION State2Desc( nState )
    SELECT CASE nState
    CASE 1
        State2Desc = "Starting (MD_SERVER_STATE_STARTING)"
    CASE 2
        State2Desc = "Started (MD_SERVER_STATE_STARTED)"
    CASE 3
        State2Desc = "Stopping (MD_SERVER_STATE_STOPPING)"
    CASE 4
        State2Desc = "Stopped (MD_SERVER_STATE_STOPPED)"
    CASE 5
        State2Desc = "Pausing (MD_SERVER_STATE_PAUSING)"
    CASE 6
        State2Desc = "Paused (MD_SERVER_STATE_PAUSED)"
    CASE 7
        State2Desc = "Continuing (MD_SERVER_STATE_CONTINUING)"
    CASE ELSE
        State2Desc = "Unknown state"
    END SELECT

END FUNCTION
0

IIS8: The loop that updates the bindings will override each time and only add the last in the list of domains provided. Another thought to consider, IIS requires each binding to be unique. So, instead of the loop you can simply

objWebserver.Put "ServerBindings", ArrayOfValues

objWebserver.SetInfo

It will add all the hostheaders provided

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.