Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting script's process id

Status
Not open for further replies.

boomboom

Technical User
Apr 18, 2002
2
0
0
US
I'd like to make sure there's only one copy of my VB script running, and if another one starts, kill the previous one. I thought I can could do that by writing the script's process ID to a file when the script starts, and removing the file when the script exits.
But I can't figure out how to get the process ID in the first place.
Thanks in advance!
 
You count the instances of Cscript or Wscript processes running and exit script if more than one. The following script uses WMI to determine if more than one instance of Cscript process is running and terminates the script if so:

Set objServices = _
GetObject("winmgmts:{impersonationLevel=impersonate}!root\cimv2")

'create an the instance of the Win32_Process class
Set objWMIObjects = objServices.InstancesOf("Win32_Process")


'loop through each process and list the handle and executable
For Each objWMIObject In objWMIObjects

If Instr(objWMIObject.ExecutablePath ,"CScript") > 0 Then
nWSHCount = nWSHCount + 1
End If
Next

'if number of instances of Cscript is greater than 1,
' exit script
If nWSHCount > 1 Then
Wscript.Quit
End If

WMI is included with Windows 2000 and XP, but requires a seperate download for NT4 and Win 9x.

Regards,

Stein Borge
Author of: Managing Enterprise Systems with the Windows Script Host
Over 800 pages of practical solutions oriented material, providing
detailed coverage of WSH 5.6, ADSI, WMI, ADO, CDO, FSO and much more.
sb@nyetspam.enterprisewsh.com <- remove nyetspam when E-mailing
 
Thanks for the suggestion! Unfortunately, it won't do exactly what I need. I need to allow other .vbs scripts to run. I just need to prevent multiple instances of this particular script from running.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top