This article demonstrates how to obtain Process information from a remote machine using WMI.
DL» process_list.zip
Retrieve Process Performance Counter Data from a Remote Machine
The Windows Task Manager is the most often the first tool opened when a machine or application
is not performing as it should. An Administrator can view "if" the process is running,
how much memory it is consuming as well as how many threads or handles are being consumed. For example,
the application may have a memory leak. This fact will be visible in the Task Manager (assuming the
normal / expected range is known).
These days, an Administrator may be responsible for dozens or hundreds of machines. To view the
Windows Task Manager requires the user be logged in to the machine locally or using a remote session
(like Remote Desktop or Terminal Services). The other option is to query the remote machine using WMI
(Windows Management Interface). This can easily be handled with a VBS script. The sample code
demonstrates how to achieve this task. It should be noted that the account running the script must have
permissions to access WMI on the remote machine.
' Copyright© 2004 Michael McLean
'
' Usage: cscript.exe proc_lst.vbs --server=[COMPUTERNAME]
' [COMPUTERNAME] NetBIOS, FQDN or IP address to remote computer
'
' [Last Updated: 21 Nov 2004]
' http://www.mmclean.ca
Option Explicit
On Error Resume Next
Const SUCCESS = 1, EMPTY_RESULTSET = 0, INVALID_PARAMS = -1
Const OBJECT_NOT_INITIALIZED = -2, INVALID_COMPUTERNAME = -3
Dim lstArgs, sComputer, oWMIService, colProcesses, oProc
Set lstArgs = WScript.Arguments
If lstArgs.Count = 1 Then
sComputer = Trim( lstArgs( 0 ))
Else
WScript.Echo " Usage: cscript.exe proc_lst.vbs --server=[COMPUTERNAME]"
WScript.Echo vbTAB & "[COMPUTERNAME] NetBIOS, FQDN or IP address " _
& "to remote computer."
WScript.Quit( INVALID_PARAMS )
End If
sComputer = getServer( lstArgs( 0 ))
If( isaddr( sComputer ) = False ) Then
WScript.Echo( "INVALID_COMPUTERNAME" & vbCRLF _
& " [" & Trim( sComputer ) & "]" )
WScript.Quit( INVALID_COMPUTERNAME )
End If
Set oWMIService = GetObject( "winmgmts:{impersonationLevel=Impersonate}!\\" _
& sComputer & "\root\cimv2" )
If( IsEmpty( oWMIService ) = True ) Then
WScript.Echo( "OBJECT_NOT_INITIALIZED :: " & sComputer )
WScript.Quit( OBJECT_NOT_INITIALIZED )
End If
Set colProcesses = oWMIService.ExecQuery( "SELECT HandleCount, IDProcess, " _
& "Name, PercentProcessorTime, ThreadCount, WorkingSet " _
& "FROM Win32_PerfRawData_PerfProc_Process" )
If( IsEmpty( colProcesses ) = True ) Then
WScript.Echo( "EMPTY_RESULTSET :: Process Collection" )
WScript.Quit( EMPTY_RESULTSET )
End If
For Each oProc in colProcesses
WScript.Echo( "Process Name [" & LCase( oProc.Name ) & "]" )
WScript.Echo( " Process ID (pid) = " & oProc.IDProcess )
WScript.Echo( " CPU Time = " & fmtNSTime( oProc.PercentProcessorTime ))
WScript.Echo( " Mem Usage = " & FormatNumber( oProc.WorkingSet / 1024, 0 ) _
& " KB" )
WScript.Echo( " Handles = " & oProc.HandleCount )
WScript.Echo( " Threads = " & oProc.ThreadCount )
WScript.Echo( "" )
Next
WScript.Quit( SUCCESS )
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' String Functions
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function fmtNSTime( ByVal nProcTime100NS )
Dim ss, mm, hh
nProcTime100NS = ( nProcTime100NS / 10000000 ) ' convert to seconds
mm = Int( nProcTime100NS / 60 )
ss = nProcTime100NS mod 60
hh = Int( mm / 60 )
mm = mm mod 60
If( Len( hh ) < 2 ) Then
fmtNSTime = ZeroPad( hh, 2 ) & ":" & ZeroPad( mm, 2 ) & ":" & ZeroPad( ss, 2 )
Else
fmtNSTime = hh & ":" & ZeroPad( mm, 2 ) & ":" & ZeroPad( ss, 2 )
End If
End Function
Function ZeroPad( ByVal sNumber, ByVal nPadFactor )
ZeroPad = Right( String( nPadFactor, "0" ) & Trim( sNumber ), nPadFactor )
End Function
Function getServer( ByVal svrName )
Dim sName, oRegEx
Set oRegEx = New RegExp
oRegEx.Pattern = "^--server="
oRegEx.Global = True
oRegEx.IgnoreCase = True
' Test to see if parameter token is valid.
If( oRegEx.Test( svrName) = True ) Then
sName = oRegEx.Replace( svrName, "" )
Else
displaySyntax()
WScript.Quit( INVALID_PARAMS )
End If
getServer = sName
Set oRegEx = Nothing
End Function
' Function to determine if string is a valid IP address or domain name.
Function isaddr( ByVal intxt )
If(( isip( intxt ) = True ) OR ( isdomain( intxt) = True )) Then
isaddr = True
Else
isaddr = False
End If
End Function
' Function to determine if string is a valid IP address.
Function isip( ByVal intxt )
Dim oRegEx
Set oRegEx = New RegExp
oRegEx.Pattern = "^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\."_
& "(\d|[01]?\d\d|2[0-4]\d|25[0-5])\." _
& "(\d|[01]?\d\d|2[0-4]\d|25[0-5])\." _
& "(\d|[01]?\d\d|2[0-4]\d|25[0-5])$"
oRegEx.Global = True
oRegEx.IgnoreCase = True
isip = oRegEx.Test( intxt )
Set oRegEx = Nothing
End Function
' Function to determine if string is a valid domain name.
Function isdomain( ByVal intxt )
Dim bok, retVal, oRegEx, dnsLabel, ele
retVal = True
bok = True
' Ensure string does nto start or end with a '.'
If(( bok = True ) AND (( Left( intxt, 1 ) = "." ) OR ( Right( intxt, 1 ) = "." ))) Then
retVal = False
bok = False
End If
' String must be less than 256 characters and greater than 1 character.
If(( bok = True ) AND (( Len( intxt ) > 255 ) OR ( Len( intxt ) < 2 ))) Then
retVal = False
bok = False
End If
' Test for invalid characters in domain name.
If( bok = True ) Then
Set oRegEx = New RegExp
oRegEx.Pattern = "[^A-Za-z0-9\.\-\_]"
oRegEx.Global = True
oRegEx.IgnoreCase = True
' Invalid characters found in string.
If( oRegEx.Test( intxt ) = True ) Then
retVal = False
End If
Set oRegEx = Nothing
End If
' Split each component in domain name.
' Ensure string length is less than 64 characters and the string is not a number.
If( bok = True ) Then
dnsLabel = Split( intxt, "." )
For Each ele In dnsLabel
If(( Len( ele ) > 63 ) OR ( IsNumeric( ele ) = True )) Then
retVal = False
Exit For
End If
Next
End If
isdomain = retVal
End Function
