vbscript调用WMI一键式式发布网站

       作为.net开发,在window环境下,不得不熟悉些脚本语言,来减轻些日常开发中所遇到的一些繁杂的事情,比如自动发布网站,自动发布网站等等。

       WMI windows管理程序接口,可用各种与语言调用,方便起见,我选择VBscript脚本语言来实现一键式发布网站

       所需WMI对象

       Set oWebAdmin=GetObject("winmgmts:rootWebAdministration") 

      oWebAdmin 提供管理 site Application VirtualDirectory 等对象的管理,调用对应对象的所提供的方法即可实现所需功能

      获取网站名称,建立IIS后系统会自动创建一个默认网站,对应的ID为1

     

'---------------------------
'-----获取网站名称----------
'---------------------------
Sub GetSiteName()
  Set Sites=oWebAdmin.InstancesOf("Site")
  For Each site In Sites 
    If site.Id=1 Then
      strWebSiteName=site.Name
      Exit For
    End If
  next
End Sub

  建立虚拟目录,需要三个参数 应用程序路径,物理路径,网站名称 

'---------------------------
'-----创建虚拟目录----------
'---------------------------
Sub CreateVD()
 Set vds=oWebAdmin.InstancesOf("VirtualDirectory")
 For Each vd In vds
 If vd.PhysicalPath=strPyhicPath Then
    '删除应用程序
     DeleteApp strAppPath
     vd.Delete_
    Exit for
 End If
 Next
 Set vd=oWebAdmin.Get("VirtualDirectory")
 vd.Create strAppPath,"/",strPyhicPath,strWebSiteName
End Sub

 创建应用程序 ,也需要三个参数  应用程序路径,物理路径,网站名称 

'---------------------------
'-----创建应用程序----------
'---------------------------
Sub CreateApp(apppath,webSiteName,pypath)
 On Error Resume next
 App.Create apppath,webSiteName,pypath
 If Err.Number<>0 Then
  WScript.Echo "创建应用程序错误:"&apppath&"错误码:"&Err.Number
  WScript.Sleep 500
 else
 WScript.Echo "正在建立应用程序:"&apppath&"..."
 WScript.Sleep 1000
 End if
End Sub

 通过以上三个步骤即可自动创建一个虚拟目录,并转换为应用程序,根据IIS版本不同,调用WMI的对象也不同,所以以上代码只正对IIS7

 全部代码如下

Dim WshShell
Set WshShell = WScript.CreateObject("Wscript.Shell")
If LCase(Right(WScript.FullName,11))="wscript.exe" Then
 WshShell.Run "cmd /k cscript.exe //nologo " & Chr(34)& WScript.ScriptFullName & Chr(34)
 WScript.Quit
End if
strWebSiteName=""
strPyhicPath=InputBox("请输入要发布网站的路径"&vbnewline&vbnewline&"如:D:xxxxxxx"&vbNewLine&vbNewLine&"请确保是否存在网站:Default Web Site","提示")
If(strPyhicPath="") Then
 MsgBox("请输入路径")
 WScript.Quit
End if
ary=Split(strPyhicPath,"")
strAppPath="/"&ary(UBound(ary))
Set oWebAdmin=GetObject("winmgmts:rootWebAdministration") 
GetSiteName
CreateVD
Set App=oWebAdmin.Get("Application")
CreateApp strAppPath&"/Web", strWebSiteName,strPyhicPath&"Web"
CreateApp strAppPath&"/WebService", strWebSiteName,strPyhicPath&"WebService"
CreateIISAppByFile(strPyhicPath&"WebService")
WScript.Echo "处理完毕..."
WScript.Sleep(1000)

'---------------------------
'-----获取网站名称----------
'---------------------------
Sub GetSiteName()
  Set Sites=oWebAdmin.InstancesOf("Site")
  For Each site In Sites 
    If site.Id=1 Then
      strWebSiteName=site.Name
      Exit For
    End If
  next
End Sub
'---------------------------
'-----创建虚拟目录----------
'---------------------------
Sub CreateVD()
 Set vds=oWebAdmin.InstancesOf("VirtualDirectory")
 For Each vd In vds
 If vd.PhysicalPath=strPyhicPath Then
    '删除应用程序
     DeleteApp strAppPath
     vd.Delete_
    Exit for
 End If
 Next
 Set vd=oWebAdmin.Get("VirtualDirectory")
 vd.Create strAppPath,"/",strPyhicPath,strWebSiteName
End Sub
'---------------------------
'-----循环创建Webservice----
'---------------------------
Sub CreateIISAppByFile(strFolder)
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(strFolder)
    For Each x In oFolder.SubFolders
       currAppPath=strAppPath&"/WebService/"&x.Name
       CreateApp currAppPath,strWebSiteName,x.Path
    Next
End Sub

'---------------------------
'-----创建应用程序----------
'---------------------------
Sub CreateApp(apppath,webSiteName,pypath)
 On Error Resume next
 App.Create apppath,webSiteName,pypath
 If Err.Number<>0 Then
  WScript.Echo "创建应用程序错误:"&apppath&"错误码:"&Err.Number
  WScript.Sleep 500
 else
 WScript.Echo "正在建立应用程序:"&apppath&"..."
 WScript.Sleep 1000
 End if
End Sub

'---------------------------
'-----删除应用程序----------
'---------------------------
Sub DeleteApp(apppath)
Set oApps = oWebAdmin.InstancesOf("Application")
Set Re=New RegExp
p=Replace(apppath,".",".")
re.Pattern=p&".*"
re.IgnoreCase=false
For Each oApp In oApps
  If re.Test(oApp.Path) then
   WScript.Echo("正在删除应用程序:"& oApp.Path)
   oApp.Delete_
   WScript.Sleep(200)
  End if
Next
End sub
原文地址:https://www.cnblogs.com/yfrs/p/3950706.html