作為.net開發,在window環境下,不得不熟悉些腳本語言,來減輕些日常開發中所遇到的一些繁雜的事情,比如自動發布網站,自動發布網站等等。
WMI windows管理程序接口,可用各種與語言調用,方便起見,我選擇VBscript腳本語言來實現一鍵式發布網站
所需WMI對象
Set oWebAdmin=GetObject("winmgmts:root/WebAdministration")
oWebAdmin 提供管理 siteapplicationVirtualDirectory 等對象的管理,調用對應對象的所提供的方法即可實現所需功能
獲取網站名稱,建立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新聞熱點
疑難解答