Vbscript calls WMI to publish a website with one click. vbscript calls wmi with one click.
As A. net developer, in the window environment, you have to be familiar with some scripting languages to alleviate some complicated things encountered in daily development, such as automatic website publishing and automatic website publishing.
WMI windows Management Program interface, which can be called in a variety of languages. For convenience, I chose VBscript language to implement one-click website publishing.
Required WMI Object
Set oWebAdmin = GetObject ("winmgmts: root \ WebAdministration ")
OWebAdmin provides management of objects such as site Application VirtualDirectory, and calls the methods provided by the corresponding object to implement the required functions.
Obtain the website name. After IIS is created, the system automatically creates a default website with the ID of 1.
'-------------------------' ----- Obtain the website name ---------- '----------------------------- 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
To create a virtual directory, three parameter application paths, physical paths, and website names are required.
'-------------------------' ----- Create a virtual directory ---------- '--------------------------- Sub CreateVD () Set vds = oWebAdmin. instancesOf ("VirtualDirectory") For Each vd In vds If vd. physicalPath = strPyhicPath then' Delete the DeleteApp strAppPath vd. delete _ Exit for End If Next Set vd = oWebAdmin. get ("VirtualDirectory") vd. create strAppPath, "/", strPyhicPath, strWebSiteNameEnd Sub
To create an application, you also need three parameter application paths, physical paths, and website names.
'-------------------------' ----- Create an application ---------- '----------------------------- Sub CreateApp (apppath, webSiteName, pypath) On Error Resume next App. create apppath, webSiteName, pypath If Err. number <> 0 Then WScript. echo "application creation error:" & apppath & "error code:" & Err. number WScript. sleep 500 else WScript. echo "creating application:" & apppath &"... "WScript. sleep 1000 End ifEnd Sub
After completing the preceding three steps, you can automatically create a virtual directory and convert it to an application. The objects that call WMI vary depending on the IIS version. Therefore, the above Code only applies to IIS7.
The Code is as follows:
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 ("Enter the path of the website to be published" & vbnewline & "e. g: D: \ xxxx \ xxx "& vbNewLine &" Make sure the website exists: Default Web Site "," prompt ") If (strPyhicPath = "") then MsgBox ("Enter the path") 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 "processing completed... "WScript. sleep (1000) '-------------------------' ----- obtain the website name ---------- '----------------------------- 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 '-------------------------' ----- create a virtual directory ---------- '----------------------------- Sub CreateVD () Set vds = oWebAdmin. instancesOf ("VirtualDirectory") For Each vd In vds If vd. physicalPath = strPyhicPath then' Delete the DeleteApp strAppPath vd. delete _ Exit for End If Next Set vd = oWebAdmin. get ("VirtualDirectory") vd. create strAppPath, "/", strPyhicPath, strWebSiteNameEnd Sub '-------------------------' ----- Create Webservice cyclically ---- 'your 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 '-------------------------' ----- create an application ---------- '----------------------------- Sub CreateApp (apppath, webSiteName, pypath) On Error Resume next App. create apppath, webSiteName, pypath If Err. number <> 0 Then WScript. echo "application creation error:" & apppath & "error code:" & Err. number WScript. sleep 500 else WScript. echo "creating application:" & apppath &"... "WScript. sleep 1000 End ifEnd Sub '-------------------------' ----- delete an application ---------- '------------------------------- 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 ("deleting application:" & oApp. path) oApp. delete _ WScript. sleep (200) End ifNextEnd sub
How can I use the code written in vbscript in vb? (For WMI)
Delete this line Set StdOut = WScript. StdOut
This is to use VBS to create strings and DWORD values
I usually use APIs to modify the Registry. If you want this module, you can send a message to me.
This is an easy-to-use code!
Private Sub commandementclick ()
Call
End Sub
Sub ()
Const HKEY_LOCAL_MACHINE = & H80000002
StrComputer = "."
'Set StdOut = WScript. StdOut
Set oReg = GetObject ("winmgmts: {impersonationLevel = impersonate }! \ "& StrComputer &" \ root \ default: StdRegProv ")
StrKeyPath = "SOFTWARE \"
StrValueName = "321312"
StrValue = "312312"
OReg. SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
StrValueName = "123"
DwValue = 82
OReg. SetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, dwValue
End Sub
How to call the hardware information in the WMI class library in MFC
These things are automatically tuned by the motherboard. You just need to connect the cables. Besides, it seems that it cannot be called.