VBS 操作 IIS

1.获得所有站点ID、名称、路径

Set ObjService=GetObject("IIS://LocalHost/W3SVC")
For Each obj3w In objservice
If IsNumeric(obj3w.Name) Then
sServerName=Obj3w.ServerComment
Set webSite = GetObject("IIS://Localhost/W3SVC/" & obj3w.Name & "/Root")
ListAllWeb = ListAllWeb & obj3w.Name & String(25-Len(obj3w.Name)," ") & obj3w.ServerComment & "(" & webSite.Path & ")" & vbCrLf
End If
Next
WScript.Echo ListAllWeb
Set ObjService=Nothing
WScript.Quit

2.安装包安装,自动建立Webservice站点,最好的操作就是通过VBS去做,IIS命令是不支持IIS6.0的。

OnErrorResumeNext
strVirtualDirectoryName="LoggerWeb"
strVirtualDirectoryPath="D:\"

strVirtualDirectoryPath=Replace(strVirtualDirectoryPath,"/","\")
Set MyFso=CreateObject("Scripting.FileSystemObject")
If MyFso.FolderExists(strVirtualDirectoryPath)=0Then
    MsgBox(strVirtualDirectoryPath&"目录不存在!无法创建虚拟目录!")
Else
    '获取站点ID
    Set ObjService=GetObject("IIS://LocalHost/W3SVC")
    ForEach obj3wIn objservice
    IfIsNumeric(obj3w.Name) Then
        Set objIIS=GetObject("IIS://localhost/W3SVC/" & obj3w.Name & "/Root")   
    EndIf
    Next

 (注意:此前我并没有加入上段内容,在实际测试发现,有的工程人员会无意间把IIS中默认的站点删掉,这样导致Webservice无法安装。因为创建的对象要具体到某个站点的ID下"IIS://localhost/W3SVC/1/Root",其中1 代表站点的ID,而默认的站点ID为1,所以我需要遍历获取所有站点中ID值最小的并进行Webservice的安装。当然,如果你有其它需求,改之即可)
    Set objVirtualDirectory=objIIS.Create("IISWebVirtualDir",strVirtualDirectoryName)
    If(Err.Number<>0) Then'如果虚拟目录已存在或有其它错误时则为真
        Err.Clear
        Set MyDir=objIIS.GetObject("IISWebVirtualDir",strVirtualDirectoryName) '创建虚拟目录
        If(Err.Number<>0)Then'虚拟目录不存在时则为真,说明是其他未知错误,否则为虚拟目录已存在 
            MsgBox("对不起,发生了未知错误,LoggerWeb创建失败!")
            WScript.Quit(Err.Number)
        EndIf
        objIIS.Delete"IIsObject",strVirtualDirectoryName
        '删除失败
        If (Err.Number<>0)Then
            MsgBox("覆盖"&strVirtualDirectoryName&"失败!")   
            WScript.Quit(Err.Number)
        Endif
        Set objVirtualDirectory=objIIS.Create("IISWebVirtualDir",strVirtualDirectoryName)
            objVirtualDirectory.AccessScript=True'访问脚步
            objVirtualDirectory.Path=strVirtualDirectoryPath
            objVirtualDirectory.EnableDefaultDoc=True'启用默认文档
            objVirtualDirectory.DefaultDoc="Default.aspx"
            objVirtualDirectory.AppFriendlyName=strVirtualDirectoryName'名称
            objVirtualDirectory.SetInfo
            objVirtualDirectory.AppCreateTrue
            
    Else
        objVirtualDirectory.AccessScript=True'访问脚步
        objVirtualDirectory.Path=strVirtualDirectoryPath
        objVirtualDirectory.EnableDefaultDoc=True'启用默认文档
        objVirtualDirectory.DefaultDoc="Default.aspx"
        objVirtualDirectory.AppFriendlyName=strVirtualDirectoryName'名称
        objVirtualDirectory.SetInfo
        objVirtualDirectory.AppCreateTrue
        
    EndIf
EndIf
Set MyFso=Nothing

   

3.做安装包时,需要创建站点,那么在卸载时就需要将其删除,下文通过遍历IIS站点,根据站点名称得到对应的ID号,删除此站点

Set ObjWebSite=GetObject("IIS://LocalHost/W3SVC")
ForEach objSiteIn ObjWebSite
IfIsNumeric(objSite.Name) Then '如果是数字
    If objSite.ServerComment="M3Setting"then '判断是否与站点“M3Setting”名称相同
        id=objSite.Name
        ObjWebSite.delete"IISWebServer",id  '删除此站点

    Set ObjWebSite=Nothing
    EndIf

End if

4.删除某个Webservice

Set ObjService=GetObject("IIS://LocalHost/W3SVC")
ForEach obj3wIn objservice
IfIsNumeric(obj3w.Name) Then
    Set objIIS=GetObject("IIS://localhost/W3SVC/" & obj3w.Name)   
    vID=obj3w.Name '获取站点ID
EndIf
Next

Set objVirtualDirectory=objIIS.GetObJect("IISWebVirtualDir","Root") '定位站点路径

ForEach vrIn objVirtualDirectory '遍历站点
If vr.Name="LoggerWeb"Then
    Set IISLogger=GetObject("IIS://localhost/W3SVC/" & vID &"/Root") '定位站点路径  
    IISLogger.Delete"IIsObject","LoggerWeb" '删除
EndIf
Next

ForEach vrIn objVirtualDirectory
If vr.Name="S1WebService"Then
    Set IISS1=GetObject("IIS://localhost/W3SVC/" & vID &"/Root")   
    IISS1.Delete"IIsObject","S1WebService"
EndIf
Next


5.设置IIS应用程序池

安装包安装完成后,因为客户对IIS不怎么了解,需要大量时间去指导怎么设置IIS的配置,便抽了些许时间完成一个函数,方便调用:
Function CreateAppPool(NewAppPoolName)
    Set AppPools = GetObject("IIS://localhost/W3SVC/AppPools")
    set NewPool = AppPools.Create("IIsApplicationPool",NewAppPoolName)
    If(Err.Number<>0) Then
  Err.Clear
 else
     NewPool.AppPoolIdentityType = 2'预定义账户0本地系统1本地服务2网络服务
     NewPool.Enable32BitAppOnWin64 = True
     NewPool.ManagedRuntimeVersion = "v2.0"
     NewPool.ManagedPipelineMode=Integrated
     NewPool.AppPoolAutoStart = true'自动启动此池
     NewPool.SetInfo
     Set AppPools = nothing
     set NewPool = nothing
     if err.number=0 then CreateAppPool=True
    End if
end function

 

在2步骤中,添加 objVirtualDirectory.AppPoolId="LoggerWeb" '应用程序池,可以设置创建的WebService的应用程序池.

属性详解:

http://msdn.microsoft.com/en-us/library/ms524328(v=vs.90).aspx

posted @ 2013-06-14 12:55  竹林逸轩  阅读(1112)  评论(0编辑  收藏  举报