Saturday 5 January 2013

VBScript to restrict only one instance of application is running

VBScript to restrict only one instance of application is running

For this example I have used Notepad (notepad.exe)



' Process.vbs
' VBScript to enforce single instance execution of an application
' Date : 5 Jan 2012
' Author : Nauman (Solveso)
' -------------------------------------------------------'

Option Explicit

Dim objWMIService, objProcess, colProcess, objShell
Dim strComputer, strList, strprocess, strprocess1

strComputer = "."

strprocess = "'notepad.exe'"
strprocess1 = "notepad.exe"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = " & strprocess)


For Each objProcess in colProcess
strList = objProcess.Name
Next


if strlist = strprocess1 Then
wscript.echo "Aplication "& strprocess &" is already running, only one instance is allowed, please contact your administrator."
wscript.quit
end if


Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run strprocess1
wscript.quit

<<>>

1 comment: