ActiveXperts.com » Support » ActiveXperts Network Monitor » VBScript functions

Additional VBScript functions

On this page, you find some useful VBScript functions that you can use in ActiveXperts Network Monitor.

CheckProcessEx - check how many instances of a process are running on a (remote) server

The 'CheckProcess' function checks how many of instances of the process are running on the (remote) server. Make sure that you pass 3 parameters to the function:

  • strComputer - the workstation or server that you want to monitor, for instance: "server01"
  • strProcess - the process that should be running on the (remote) workstation or server, for instance: "ax32.exe"
  • numInstances - the number of instances of the process that should run on the remote workstation or server, for instance : 3
Function CheckProcessEx( strComputer, strProcess, numInstances )
' This function checks how many of instances of the process are
' running on the (remote) server
 
On Error Resume Next
 
    Dim objWmi, objProcess, collProcesses
    Dim strQuery
    Dim numProcesses
 
    numProcesses = 0
 
    set objWmi = GetObject("winmgmts://" & strComputer )
    If( Err.Number <> 0 ) Then
        CheckProcess = retvalUnknown
        EXPLANATION = "Unable to access '" & strComputer & "'. Possible reasons: no WMI installed on the remote server, no rights to access remote WMI service, or remote server down"
        Exit Function 
    End If
 
    strQuery = "select * from win32_process"
    set collProcesses = objWmi.ExecQuery( strQuery ) 
    If( collProcesses.Count = 0 ) Then
        CheckProcess = retvalUnknown
        EXPLANATION = "Unable to list processes on computer '" & strComputer & "'"
        Exit Function       
    End If
 
    For Each objProcess in collProcesses
        If( Err.Number <> 0 ) Then
            CheckProcess = retvalUnknown
            EXPLANATION = "Unable to list processes on computer '" & strComputer & "'"
            Exit Function 
        End If
        If UCase( objProcess.Name ) = UCase( strProcess ) Then
            numProcesses = numProcesses + 1
        End If
    Next
 
    If( numProcesses = numInstances ) Then
        CheckProcess = True
        EXPLANATION = numInstances & " Processes '" & strProcess & "' is running on computer '" & strComputer & "'"
    Else
        CheckProcess = False
        EXPLANATION = "No " & numInstances & " processes '" & strProcess & "' is not running on computer '" & strComputer & "'"
    End If
 
End Function

CheckMp3Files - check existance of MP3 files in a directory

The 'CheckMp3' function checks if there are any MP3 files in a specific directory. Make sure that you pass 1 parameter to the function:

  • strDirectory - the actual directory to check (preferably a UNC), for instance: "\\server01\public\support-staff"
Option Explicit
Const retvalUnknown = 1

' ///////////////////////////////////////////////////////////////////////////////

Function CheckMp3Files( strDirectory )
   Dim numMp3Files, objFso, objFolder

   Set objFso = CreateObject("Scripting.FileSystemObject") 
   Set objFolder = objFso.GetFolder("c:\temp") 

   numMp3Files = CountMp3Files( objFolder )
   If( numMp3Files > 0 ) Then
      EXPLANATION = numMp3Files & " MP3 files detected"
      CheckMp3Files = False
   Else
      EXPLANATION = "No MP3 files detected"
      CheckMp3Files = True
   End If
End Function

' ///////////////////////////////////////////////////////////////////////////////

Function CountMp3Files( objFolder )
   Dim objSubFolders, objSubFolder
   Dim objFiles, objFile
   Dim iFileNum

   iFileNum = 0
   CountMp3Files = 0

   Set objFiles = objFolder.Files
   If objFiles.Count <> 0 Then
      For Each objFile In objFiles
         If( InStr( UCase( objFile.Name ), ".MP3" ) <> 0 ) Then
            iFileNum = iFileNum + 1
         End If
      Next
   End If

   Set objSubFolders = objFolder.SubFolders
   If objSubFolders.Count <> 0 Then
      For Each objSubFolder In objSubFolders
         iFileNum = iFileNum + CountMp3Files( objSubFolder )
      Next
   End If

   CountMp3Files = iFileNum
End Function
M