As. NET development, in the window environment, must be familiar with some scripting language, to alleviate some of the daily development of the complex things encountered, such as automatic publishing sites, automatic publishing sites and so on.
WMI Windows Hypervisor interface, available in various languages with language calls, for convenience, I choose the VBScript scripting language to implement a one-click Publishing Site
Required WMI Objects
Set owebadmin=getobject ("Winmgmts:root\webadministration")
Owebadmin provides management of objects such as the managed site application VirtualDirectory, and invokes the provided methods of the corresponding objects to achieve the required functions.
Gets the name of the Web site, and the system automatically creates a default Web site with an ID of 1 after IIS is established
'---------------------------'-----Get the site name----------'---------------------------SubGetsitename ()SetSites=owebadmin.instancesof ("Site") for eachSiteinchSitesIfSite. Id=1 ThenStrwebsitename=site. NameExit for End If NextEnd Sub
To create a virtual directory, you need three parameters application path, physical path, site name
'---------------------------'-----Create a virtual directory----------'---------------------------SubCREATEVD ()SetVds=owebadmin.instancesof ("VirtualDirectory") for eachVdinchVDSIfVd. Physicalpath=strpyhicpath Then 'Remove an applicationDeleteapp Strapppath VD. Delete_Exit for End If Next SetVd=owebadmin.get ("VirtualDirectory") Vd. Create Strapppath,"/", Strpyhicpath,strwebsitenameEnd Sub
Application path, physical path, site name, three parameters are required for creating applications
'---------------------------'-----Create an application----------'---------------------------SubCreateapp (Apppath,websitename,pypath) on Error Resume Nextapp.create Apppath,websitename,pypathIfErr.number<>0 ThenWScript.Echo"Create Application Error:"&apppath&"Error code:"&Err.Number Wscript.Sleep - ElseWScript.Echo"Building Application:"&apppath&"..."Wscript.Sleep + End ifEnd Sub
With the three steps above, you can automatically create a virtual directory and convert it to an application, and depending on the version of IIS, the objects that call WMI are different, so the code above is only IIS7
The entire code is as follows
DimWshShellSetWshShell = WScript.CreateObject ("Wscript.Shell")If LCase( Right(Wscript.fullname, One))="Wscript.exe" ThenWshshell.run"cmd/k cscript.exe//nologo"&Chr( the) & Wscript.scriptfullname &Chr( the) Wscript.QuitEnd ifStrwebsitename=""Strpyhicpath=InputBox("Please enter the path of the site you want to publish"&vbnewline&vbnewline&"such as: D:\xxxx\xxx"&vbNewLine&vbNewLine&"make sure that the Web site is present: Default Web","Tips")If(strpyhicpath="") Then MsgBox("Please enter a path") Wscript.QuitEnd ifary=Split(Strpyhicpath,"\") Strapppath="/"&ary (UBound(ary))Setowebadmin=GetObject("winmgmts:root\webadministration") GETSITENAMECREATEVDSetApp=owebadmin.get ("Application") Createapp Strapppath&"/web", strwebsitename,strpyhicpath&"\web"Createapp Strapppath&"/webservice", strwebsitename,strpyhicpath&"\webservice"Createiisappbyfile (Strpyhicpath&"\webservice") WScript.Echo"Processing finished ..."Wscript.Sleep ( +)'---------------------------'-----Get the site name----------'---------------------------SubGetsitename ()SetSites=owebadmin.instancesof ("Site") for eachSiteinchSitesIfSite. Id=1 ThenStrwebsitename=site. NameExit for End If NextEnd Sub'---------------------------'-----Create a virtual directory----------'---------------------------SubCREATEVD ()SetVds=owebadmin.instancesof ("VirtualDirectory") for eachVdinchVDSIfVd. Physicalpath=strpyhicpath Then 'Remove an applicationDeleteapp Strapppath VD. Delete_Exit for End If Next SetVd=owebadmin.get ("VirtualDirectory") Vd. Create Strapppath,"/", Strpyhicpath,strwebsitenameEnd Sub'---------------------------'Create a WebService---------loop'---------------------------Subcreateiisappbyfile (strfolder)SetoFSO =CreateObject("Scripting.FileSystemObject") SetOfolder =Ofso.getfolder (strfolder) for eachXinchofolder.subfolders Currapppath=strapppath&"/webservice/"&x.name Createapp Currapppath,strwebsitename,x.pathNextEnd Sub'---------------------------'-----Create an application----------'---------------------------SubCreateapp (Apppath,websitename,pypath) on Error Resume Nextapp.create Apppath,websitename,pypathIfErr.number<>0 ThenWScript.Echo"Create Application Error:"&apppath&"Error code:"&Err.Number Wscript.Sleep - ElseWScript.Echo"Building Application:"&apppath&"..."Wscript.Sleep + End ifEnd Sub'---------------------------'-----Remove an application----------'---------------------------SubDeleteapp (AppPath)SetOapps = Owebadmin.instancesof ("Application")SetRe=Newregexpp=Replace(AppPath,".","\.") Re. Pattern=p&".*"Re. IgnoreCase=false for eachOappinchOappsIfRe. Test (Oapp.path) ThenWScript.Echo ("Removing application:"&oapp.path) Oapp.delete_ Wscript.Sleep ( $) End ifNextEnd Sub
VBScript invokes WMI one-click Publishing site