Windows

iis vbscript 刪除所有站點綁定並替換為新綁定列表

  • October 10, 2017

我有一個主機,它關聯了 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

經過大量孤獨的研究,我希望這個答案能真正幫助其他人節省時間。

如果您有任何問題,請隨時發表評論,它並不漂亮,我相信它可以寫得更好,但對於一項任務它是有效的。

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

引用自:https://serverfault.com/questions/336810