Skip to content

How to tell if another instance of a VBS script is running

The following VBS function will allow you to check if another instance of your script is running. Note, I have only tested this for one user.

Function OtherInstances()
  ' This function determines if there are other instances of this script running. If other instances are running it returns TRUE, otherwise it returnes FALSE
 
  Dim strComputer, objWMIService, colItems, objItem, Count
 
  strComputer = "."
  Count = 0
  Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set colItems = objWMIService.ExecQuery _
      ("Select * from Win32_Process Where Name = 'cscript.exe'" & _
          " OR Name = 'wscript.exe'")
  For Each objItem in colItems
      If InStr(1, LCase(objItem.CommandLine), LCase(Wscript.ScriptName)) > 0 Then
        Count = Count + 1	
	  End If
  Next
 
  If Count > 1 Then
    OtherInstances = TRUE
  Else
    OtherInstances = FALSE 
  End If
End Function

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*