标签归档:IIsWebServer

根据域名列表,停域名对应的站点

'****************************************************************
' 功能:根据域名列表,停止IIS上域名所对应的主机
' 域名列表格式:文本,一个域名一行
'    abc.com
'    abc.cn
' 时间:2009-3-18
'****************************************************************
Dim strFileName
strFileName = "domainlist.txt"    '更改为要关闭站点的域名列表文件名
Call ReadEntireFile(strFileName)<!--more-->

'**********************************************************
' 从文本中读取域名列表
'**********************************************************
Sub ReadEntireFile(filespec)
    'On Error Resume Next
    Const ForReading = 1
       Dim fso, theFile, retstring
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set theFile = fso.OpenTextFile(filespec, ForReading, False)
    LogToFile(Date & "开始关闭站点")
    LogToFile("*************************************")
       Do While theFile.AtEndOfLine <> True
          retstring = Trim(theFile.ReadLine)
        LogToFile(Date & "关闭站点["& retstring &"]")
        If Len(retstring) > 3 Then
            intSiteID = GetSiteIDByDomain(retstring)
            If intSiteID > 0 Then
                LogToFile("站点ID为["& intSiteID &"]")
                PauseSiteByID(intSiteID)
                LogToFile("关闭站点["& retstring &"]完成")
                LogToFile("*************************************")
            Else
                LogToFile("站点["& retstring &"]不存在!")
                LogToFile("*************************************")
            End If
            
        Else
            theFile.SkipLine
        End If
       Loop
    theFile.Close
    LogToFile("站点关闭完成!")
    Wscript.echo "站点关闭完成!"
End Sub
'****************************************************
' 根据域名获取域名在IIS上的标识符
'****************************************************
Function GetSiteIDByDomain(DomainName)
    Set objW3svc = GetObject("IIS://LocalHost/W3svc")
    For Each objChildObject In objW3svc
        If (Err.Number <> 0) Then Exit For
        If IsNumeric(objChildObject.Name) = True Then '判断objChildObject.Name是不是数字
            'Wscript.echo objChildObject.Name
            Set objIIs = objW3svc.GetObject("IIsWebServer", objChildObject.Name) '建立IIS虚拟站点对像
            If Err.Number <> 0 Then
                Exit For
                Wscript.Echo "Error # " & CStr(Err.Number) & " " & Err.Description
                Err.Clear()
                Wscript.Quit
                GetSiteIDByDomain = 0
            End If
            arrServerBindings = objIIs.ServerBindings '把IIS虚拟站点绑定的IP、端口、域名的值放入数组ServerBindings
            For i = LBound(arrServerBindings) To UBound(arrServerBindings)
                '59.57.251.55:80:mssql.test3.com
                'Wscript.echo arrServerBindings(i)
                arrItems = Split(arrServerBindings(i),":")
                If UBound(arrItems)=2 Then
                    If CStr(Trim(arrItems(2))) = CStr(Trim(DomainName)) Then
                        GetSiteIDByDomain = objChildObject.Name
                        Exit For
                    End If
                Else
                    GetSiteIDByDomain = 0
                End IF
            Next
        End If
    Next
    Set objW3svc = Nothing
    Set objIIs = Nothing
End Function

'****************************************************
' 根据标识符,停止标识符对应的站点
'****************************************************
Sub PauseSiteByID(SiteID)

    strComputer = "."
    Set objWMIService = GetObject _
        ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
            & strComputer & "\root\microsoftiisv2")

    Set colItems = objWMIService.ExecQuery _
        ("Select * From IIsWebServer Where Name = " & _
            "'W3SVC/"& SiteID &"'")

    For Each objItem in colItems
        'objItem.ServerState
        ' 4 -- stopped
        ' 2 -- running
        ' 6 -- paused
        objItem.Stop
    Next

End Sub

'****************************************************
' 日志记录
'****************************************************
Sub LogToFile(strContent)
    Const ForReading = 1, ForWriting = 2,ForAppending = 8
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile("log_" & Date & ".txt", ForAppending, True)
    f.WriteLine Date() & " " & Time & " " & strContent
    
    Set fso=Nothing
End Sub