首页 > 代码库 > vbscript调用WMI一键式式发布网站

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

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

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

       所需WMI对象

       Set oWebAdmin=GetObject("winmgmts:root\WebAdministration") 

      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  nextEnd 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,strWebSiteNameEnd 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 ifEnd Sub

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

 全部代码如下

Dim WshShellSet 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.QuitEnd ifstrWebSiteName=""strPyhicPath=InputBox("请输入要发布网站的路径"&vbnewline&vbnewline&"如:D:\xxxx\xxx"&vbNewLine&vbNewLine&"请确保是否存在网站:Default Web Site","提示")If(strPyhicPath="") Then MsgBox("请输入路径") WScript.QuitEnd ifary=Split(strPyhicPath,"\")strAppPath="/"&ary(UBound(ary))Set oWebAdmin=GetObject("winmgmts:root\WebAdministration") GetSiteNameCreateVDSet 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  nextEnd 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,strWebSiteNameEnd 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    NextEnd 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 ifEnd Sub--------------------------------删除应用程序-------------------------------------Sub DeleteApp(apppath)Set oApps = oWebAdmin.InstancesOf("Application")Set Re=New RegExpp=Replace(apppath,".","\.")re.Pattern=p&".*"re.IgnoreCase=falseFor Each oApp In oApps  If re.Test(oApp.Path) then   WScript.Echo("正在删除应用程序:"& oApp.Path)   oApp.Delete_   WScript.Sleep(200)  End ifNextEnd sub

 

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