WMI - Memory, Disk Space, Stop/Start Services

By Robbe D. Morris

Printer Friendly Version

Robbe Morris
Robbe & Melisa Morris
There are times when we need to view or manipulate various system attributes and services.  Here is a sample Active Server Pages or ASP page that demonstrates how to use WMI to view current system memory, disk space, and services.  It also provides a function to start and stop individual services.
WMI is capable of doing much more than is demonstrated here such as a system shutdown or reboot.  You'll want to visit MSDN for more details.


Sample Code
  
 <% 

Response.Buffer = True 
Response.AddHeader "cache-control", "private" 
Response.AddHeader "pragma", "no-cache" 
Response.ExpiresAbsolute = #January 1, 1990 00:00:01# 
Response.Expires=0 
   

Const sT1 = "<td align="left" class="clsFieldLabel" nowrap>" 
Const sT2 = "<td align="right" class="clsBodyText" nowrap>" 
Const sTable = "<table border="0" width='90%' cellspacing=0 cellpadding=0>" 
Const sPgName = "/myprotectedfolder/WMI.asp" 

Sub GetServerDetails() 
     Dim oWMI 
     Dim sURL 
     Set oWMI = getObject("winmgmts:") 

     if request.queryString("action") = "" then 
        Call WriteHTMLHeader("30",sPgName) 
        Call WriteServerMemory(oWMI) 
        Call WriteHTMLFooter() 
    else 
        Call WriteHTMLHeader("10",sPgName) 
     Call ToggleService("10",oWMI) 
        Call WriteHTMLFooter() 
     end if 

     Set oWMI = nothing 

End Sub 

Sub ToggleService(sSeconds,oWMI) 

Dim nReturn 
    Dim sAction 
    Dim sService 
    Dim oWMIServices 
    Dim oWMIService 
    Dim sTD 
   
sAction = request.queryString("action") 
sService = request.queryString("service") 

    sTD = "<tr><td align="center" class="clsBodyText">" 
     
   With Response 

              .write "<table align="center" border="0" cellspacing=0 cellpadding=0 width='80%'>" 
              .write "<tr><td><br></td></tr>" 
              .write "<tr><td><br></td></tr>" 
              .write "<tr><td><br></td></tr>" 
              .write "<tr><td align="center" class=clsLabelHeading>Attempting to " & sAction & " the " & sService & " service...</td></tr>" 
              .write "<tr><td><br></td></tr>" 

set oWMIServices = oWMI.execQuery("select * from win32_service where name='" & sService & "'") 

for each oWMIService in oWMIServices 

     Select Case sAction 
               Case "start" :  nReturn = oWMIService.startService 
       Case "stop" :  nReturn = oWMIService.stopService 
End Select 

Select Case nReturn 
       Case 0 : .write sTD & "The request was accepted and is being carried out.</td></tr>" 
   Case 2 : .write sTD & "You do not have sufficient access privileges to execute this command.</td></tr>" 
   Case 3 : .write sTD & "The service cannot be stopped because dependent services are still running.</td></tr>" 
   Case 7 : .write sTD & "The service did not respond to the start request within the timeout period.</td></tr>" 
       Case else: .write sTD & "Err Number:?" & nReturn & " occured.  Please see MSDN Documentation for details.</td></tr>" 
End Select 

           .write "<tr><td><br></td></tr>" 
           .write sTD & "You will automatically be returned to the <a href=" & sPgName & " class=linkroll >Services Table</a> in " 
           .write sSeconds & " seconds.</td></tr>" 

next 

        .write "</table>" 
    End With 

Set oWMIServices = nothing 
Set oWMIService = nothing 

end sub 

Function GetWMIDate(sVal) 

        Dim YY 
        Dim MM 
        Dim DD 
        Dim hh 
        Dim mi 
        Dim ss 

         YY = Trim(Mid(sVal,1,4)) 
         MM = Trim(Mid(sVal,5,2)) 
         DD = Trim(Mid(sVal,7,2)) 
         hh = Trim(Mid(sVal,9,2)) 
         mi = Trim(Mid(sVal,11,2)) 
         ss = Trim(Mid(sVal,13,2)) 

        GetWMIDate = MM & "/" & DD & "/" & YY & "  " & hh & ":" & mi & ":" & ss 
End Function 


Function SetBytes(Bytes,fKB) 

if fKB=True then Bytes = Bytes * 1024 

If Bytes >= 1073741824 Then 
    SetBytes = FormatNumber((Bytes / 1024 / 1024 / 1024),2,,-1,-1) & " GB" 
ElseIf Bytes >= 1048576 Then 
    SetBytes =  FormatNumber((Bytes / 1024 / 1024),2,,-1,-1) & " MB" 
ElseIf Bytes >= 1024 Then 
    SetBytes = FormatNumber((Bytes / 1024),2,,-1,-1) & " KB" 
ElseIf Bytes < 1024 Then 
    SetBytes = Bytes & " Bytes" 
End If 

End Function 



Sub WriteServerMemory(oWMI) 

        Dim oTmp 
        Dim oCtrl 
        Dim sTotMemory 
        Dim sSrvAction 
       
        on error resume next 

         With Response 

                 .write sTable 

                 Set oTmp = oWMI.InstancesOf("win32_computersystem") 
   
                 For Each oCtrl in oTmp 
                       sTotMemory = oCtrl.totalphysicalmemory 
                 Next 
   
                 Set oTmp = nothing 

                set oTmp = oWMI.InstancesOf("win32_operatingsystem") 
   
                For Each oCtrl in oTmp 
                     
                      .write "<tr><td align="center" colspan="2" class=clsLabelHeading><u>Server Status</u></td></tr>" 
                      .write "<tr><td align="center" colspan="2" class="clsBodyText">" & Now() & "</td></tr>" 
                      .write "<tr>" & sT1 & "<br></td>" & sT2 & "</td></tr>" 
                      .write "<tr>" & sT1 & "Service Pack</td>" & sT2 & oCtrl.csdversion  & "</td></tr>" 
                      .write "<tr>" & sT1 & "Last Reboot</td>" & sT2 & GetWMIDate(oCtrl.lastbootuptime)  & "</td></tr>" 
                      .write "<tr>" & sT1 & "<br></td>" & sT2 & "</td></tr>" 
                      .write "<tr>" & sT1 & "Total Physical Memory</td>" & sT2 & SetBytes(sTotMemory,false)  & "</td></tr>" 
                      .write "<tr>" & sT1 & "Free Memory</td>" & sT2 &  SetBytes(oCtrl.freephysicalmemory,true)  & "</td></tr>" 
                      .write "<tr>" & sT1 & "Total Virtual Memory</td>" & sT2 & SetBytes(oCtrl.totalvirtualmemorysize,false)  & "</td></tr>" 
                      .write "<tr>" & sT1 & "Free Virtual Memory</td>" & sT2 & SetBytes(oCtrl.freevirtualmemory,false) &"</td></tr>" 
                      .write "<tr>" & sT1 & "# Processes Running</td>" & sT2 & oCtrl.numberofprocesses  & "</td></tr>" 
                       
                Next 

                Set oTmp = nothing 

               Set oTmp = oWMI.InstancesOf("win32_logicaldisk") 
   
                For Each oCtrl in oTmp 

                      if oCtrl.drivetype = 3 then 
                         .write "<tr>" & sT1 & "<br></td>" & sT2 & "</td></tr>" 
                         .write "<tr>" & sT1 & "Drive</td>" & sT2 & oCtrl.name  & "</td></tr>" 
                         .write "<tr>" & sT1 & "Label</td>" & sT2 & oCtrl.volumename  & "</td></tr>" 
                   
                         .write "<tr>" & sT1 & "File System</td>" & sT2 & oCtrl.FileSystem  & "</td></tr>" 
                         .write "<tr>" & sT1 & "File System Size</td>" & sT2 & SetBytes(oCtrl.size,false) & "</td></tr>" 
                         .write "<tr>" & sT1 & "Free Disk Space</td>" & sT2 & SetBytes(oCtrl.freespace,false)  & "</td></tr>" 
                     
                      end if 

                Next 

                Set oTmp = nothing 

                Set oTmp = oWMI.InstancesOf("win32_process") 

                .write "<tr>" & sT1 & "<br></td>" & sT2 & "</td></tr>" 
             
                .write "<tr><td align="center" colspan="2" class=clsLabelHeading><u>Processes</u></td></tr>" 
                .write "<tr><td align="center" colspan="2" class="clsBodyText">" & Now() & "</td></tr>" 
                .write "<tr>" & sT1 & "<br></td>" & sT2 & "</td></tr>" 

                .write "<tr><td colspan="2" align="center">" 
                .write "<table border="0" cellspacing=1 cellpadding=1 width='100%'>" 
                      .write "<tr>" 
                      .write "<td align="left" class="clsFieldLabel"><u>Name</u></td>" 
                      .write "<td align="left" class="clsFieldLabel"><u>FileName</u></td>" 
                      .write "<td align="right" class="clsFieldLabel"><u>Memory</u></td>" 
                      .write "</tr>" 
                      .write "<tr>" & sT1 & "</td>" & sT2 & "</td></tr>" 
                 
                For Each oCtrl in oTmp 
                      .write "<tr>" 
                      .write sT1 & oCtrl.Name & "</td>" 
                      .write "<td align="left" class="clsBodyText" >" &  oCtrl.executablePath & "</td>" 
                      .write "<td align="right" class="clsBodyText" >" &  SetBytes(oCtrl.WorkingSetSize,false)  & "</td>" 
                      .write "</tr>" 
                Next 

                .write "</table>" 
                .write "</td></tr>" 

                Set oTmp = nothing 


   
                Set oTmp = oWMI.InstancesOf("win32_service") 
   
                         .write "<tr><td colspan="2">" 
                         .write "<table border="0" cellspacing="2" cellpadding="2">" 
                         .write "<tr><td colspan=6><br></td></tr>" 
                         .write "<tr><td align="center" class=clsLabelHeading colspan=6><u>Active Services</u></td></tr>" 
                         .write "<tr><td align="center" class="clsBodyText" colspan=6>" & Now() & "</td></tr>" 
                         .write "<tr><td colspan=6><br></td></tr>" 
                         .write "<tr>" 
                         .write "<td align="left" class="clsFieldLabel" nowrap><u>Display Name</u></td>" 
                         .write "<td align="left" class="clsFieldLabel" nowrap><u>State</u></td>" 
                         .write "<td align="left" class="clsFieldLabel" nowrap><u>Start Mode</u></td>" 
                         .write "<td align="left" class="clsFieldLabel" nowrap><u>Service Type</u></td>" 
                         .write "<td align="left" class="clsFieldLabel" nowrap><u>Status</u></td>" 
                         .write "<td align="left" class="clsFieldLabel" nowrap><u>Start Name</u></td>" 
                         .write "</tr>" 
                         .write "<tr><td align="left" class="clsFieldLabel" colspan=6></td></tr>" 

                      For Each oCtrl in oTmp 
             
                         sSrvAction = "start" 
                         if ucase(oCtrl.State) = "RUNNING" then sSrvAction = "stop" 

                         .write "<tr>" 
                         .write "<td align="left" class="clsBodyText">" & oCtrl.DisplayName  & "</td>" 
                         
                         .write "<td align="left" class="clsFieldLabel">" 
                         .write "<a href=" & sPgName & "?action=" & sSrvAction & "&service=" & oCtrl.Name  & " class=linkroll>" & oCtrl.State & "</td>" 
                       
                         .write "<td align="center" class="clsBodyText">" & oCtrl.StartMode  & "</td>" 
                         .write "<td align="center" class="clsBodyText">" & Replace(oCtrl.ServiceType,"Process","")  & "</td>" 
                         .write "<td align="center" class="clsBodyText">" & oCtrl.Status  & "</td>" 
                         .write "<td align="left" class="clsBodyText">" & oCtrl.StartName  & "</td>" 
                         .write "</tr>" 
                     
                       Next 

                         .write "</table>" 
                         .write "</td></tr>" 
                Set oTmp = nothing 
   
                .write "</table>" 

        End With 
     
End Sub   

Sub WriteHTMLHeader(sSeconds,sURL) 

        With Response 
                .write "<HTML><meta http-equiv=refresh content='" & sSeconds & "; url=" & sURL & "'>" 
                .write "<BODY><style type=text/css>" 
                .write "TD.content { BACKGROUND-COLOR: #ffffff }" 
                .write "    .clsBodyText { FONT-WEIGHT: normal; FONT-SIZE: 8pt; FONT-FAMILY: Verdana; font-style: normal  } " 
                .write "   .clsFieldLabel {FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana; } " 
                .write "   .clsLabelHeading{FONT-WEIGHT: bold;FONT-SIZE: 12pt;COLOR: black; FONT-FAMILY: Verdana;} " 
                .write "   .linkroll { font-family:Arial; font-size:8pt; font-style:normal; font-weight:bold; letter-spacing:normal; " 
                .write "                 text-decoration:none; text-transform:none; color:#858BFD; }  " 
                .write "   A.linkroll:hover { font-weight:bold;    color:black }</style>" 
        End With 

End Sub 

Sub WriteHTMLFooter() 
       response.write "</BODY></HTML>" 
End Sub 

Call GetServerDetails() 

%> 


Robbe has been a Microsoft MVP in C# since 2004.  He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.