VBScript invokes WMI one-click Publishing site

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.