Home > Articles > Operating Systems, Server > Microsoft Servers

  • Print
  • + Share This

Adding Some Extras

The WMIProcCheck.Process component also provides the ability to start and stop a service using its StartProc and StopProc methods. Both methods accept the index into the processes array in order to indicate which process to stop. Rather than use ExecQuery and WQL to obtain a reference to the SWbemObject object, these methods use the alternative Get method of the SWbemServices object. The Get method is passed the equivalent of the WHERE clause, which contains the class name and returns the reference. In the following code snippet the variable ServiceName is assumed to hold the name of an NT Service to query for.

Dim ServiceObject As SWbemObject
Dim ServiceName As String

Set ServiceObject = objServices.Get("Win32_Service='" & ServiceName & "'")

After the reference is obtained, you can once again call a method on the class(such as StartService) dynamically through automation. Note that for this component, the StartProc and StopProc methods simply return as False if the process is not an NT service.

The custom component's second feature is that it can return the information about the processes in an XML string as well as in an array. This is obviously beneficial for returning information through an ASP page that is to be displayed in MSIE. The GetXML method of the component uses the MSXML parser to create a simple XML document by looping through the array and creating elements with the appropriate attributes for each process as shown in the Listing below. The resulting XML for the notepad and SQL Server processes (discussed previously) follows:

     <Process DisplayName="notepad" ProcName="notepad.exe" 
        Running="-1" IsService="0" PathMode="C:\WINNT\System32\notepad.exe"
        Account="SSOSA\Administrator" ServerName="ssosa"/>   
     <Process DisplayName="SQL Server" ProcName="MSSQLServer" 
       Running="-1" IsService="-1" PathMode="Auto" Account="LocalSystem"

Get the XML. The GetXML public function uses the MSXML parser to create an XML document from the internal array.

Public Function GetXML() As String

' Return the data as XML
Dim xmlDoc As DOMDocument
Dim xmlElem As IXMLDOMElement
Dim xmlSubElem As IXMLDOMElement
Dim i As Long

If IsArray(mProcesses) Then

    Set xmlDoc = New DOMDocument

    ' Create the top level element
    Set xmlElem = xmlDoc.createElement("ProcessData")

    ' Loop through the processes and create the sub elements
    For i = 0 To UBound(mProcesses, 2)
        Set xmlSubElem = xmlDoc.createElement("Process")
        xmlSubElem.setAttribute "DisplayName", mProcesses(0, i)
        xmlSubElem.setAttribute "ProcName", mProcesses(1, i)
        xmlSubElem.setAttribute "Running", mProcesses(2, i)
        xmlSubElem.setAttribute "IsService", mProcesses(3, i)
        xmlSubElem.setAttribute "PathMode", mProcesses(4, i)
        xmlSubElem.setAttribute "Account", mProcesses(5, i)
        xmlSubElem.setAttribute "ServerName", mProcesses(6, i)
        xmlElem.appendChild xmlSubElem
        Set xmlSubElem = Nothing
    Next i
    ' Append the high level node
    xmlDoc.appendChild xmlElem
    GetXML = xmlDoc.xml
    Set xmlDoc = Nothing
    Set xmlDoc = Nothing
    Err.Raise vbObjectError + 5002, "GetXML", "No processes to enumerate"
End If
End Function

In this case, both notepad and SQL Server were found running. Keep in mind that the DMTF is adding XML support to the specification, so look for Microsoft to add intrinsic XML support to the WMI interfaces shortly.

Although this article is not an exhaustive discussion of WMI, this small example is intended to help you understand you can use WMI to your advantage.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.