iis vbscript 删除所有站点绑定并替换为新绑定列表

iis vbscript 删除所有站点绑定并替换为新绑定列表

我有一台主机,与之关联的绑定超过 500 个,我想先清除该主机的所有绑定,然后从文件中的绑定列表中替换它们。填充格式将是要分配的绑定的逗号分隔列表,即 site.com、site1.com 等。

到目前为止,我首先开始寻找我想要执行此任务的主机,然后尝试删除所有绑定......基本上我试图::1:删除当前分配的所有绑定,2:从文件添加绑定。

更新 1:我现在将每个想要删除的绑定写入一个文件....现在我怎样才能删除实际的绑定...

更新 2:经过长时间的折腾,我终于清除了绑定,并将新绑定添加到了选定的域。我所要做的就是在文件中插入一个插入视图,以读取要添加的绑定列表……

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

答案1

经过大量孤独的研究,我希望这个答案确实可以帮助其他人节省时间。

如果您有任何疑问,请随时发表评论,它并不漂亮,我相信它可以写得更好,但对于一项任务来说它是有效的。

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

答案2

IIS8:更新绑定的循环每次都会覆盖,并且只添加提供的域列表中的最后一个域。另一个需要考虑的问题是,IIS 要求每个绑定都是唯一的。因此,您可以简单地使用以下方法代替循环:

objWebserver.Put "ServerBindings", ArrayOfValues

objWebserver.SetInfo

它将添加所有提供的主机头

相关内容