# Thread: [RESOLVED]Check running program, start if not running VBscript.

1. ## [RESOLVED]Check running program, start if not running VBscript.

Hello!

First of all, im totaly new to this and i dont know if im posting this in the right forum. Admin can delete/move this post if it's in the wrong place.

I have a Windows 2000 server (Yes i know it's lighyears old but i ) where i run 2 small programs that collects data from different places.
One of those programs chrashes 2-3 times a month and therefore i was looking after a script that could check if the program is running and run it if it isnt.
Easy as pie, so I thought.
Googled alot after different script, all that i found and tried got me different error messages and didnt work in Win 2000, most of them worked on my Win10 PC.
Then I foudn this VBscript that didn't produce any error instead nothing happened. so i thought maybe this could work if it just got modified a bit?
And that is why I need your help here guys, is it possible to manage this in Win2000 ?

Script:

Dim objWMIService, colItems, objItem, strComputer, strFlashEXEFile
Dim count

strFlashEXEFileName = "BarkStat.exe"
strFlashEXEFilePath = "C:\BarkStat\BarkStat" & strFlashEXEFileName
strComputer = "."

Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2")
Set colItems = objWMIService.InstancesOf("Win32_Process")

count = 0

For Each objItem In colItems
If objItem.Name = strFlashEXEFileName Then
count = count + 1
Else
End If
Next

Set objWMIService = Nothing
Set colItems = Nothing

If count = 0 Then

Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
Return = oShell.run(strFlashEXEFilePath,3, false)
Set oShell = Nothing

End If

2. ## Re: Check running program, start if not running VBscript.

You can write a .bat file (not .cmd, this is important) like this

Code:
@echo off
:restart
c:\path\to\MyApp.exe
goto :restart
. . . and run this auto-restarting script instead of launching MyApp.exe directly.

cheers,
</wqw>

3. ## Re: Check running program, start if not running VBscript.

Oh... why didn't i even thought about the restart command.
I overdid it a bit then. LOL
It worked flawless, thanks!

Br,
Theatre

4. ## Re: Check running program, start if not running VBscript.

strFlashEXEFilePath = "C:\BarkStat\BarkStat" & strFlashEXEFileName
i would assume the should be a \ between the file path and file name, but that would also mean the full file path to be C:\BarkStat\BarkStat\BarkStat.exe, which seems may be an extra folder depth

i rewrote some and put in some testing, the code, tested against some .exe file on my machine, runs right through without error and i am sure it should work correctly
you can remove the testing when you are sure it is doing as required

Code:
Dim objWMIService, colItems, objItem, strComputer, strFlashEXEFile
Dim count

strFlashEXEFileName = "BarkStat.exe"

strFlashEXEFilePath = "C:\BarkStat\BarkStat" & strFlashEXEFileName

strComputer = "."

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")   ' impersonation seems essential on w10, but probably not required on xp or other earlier versions
'Set objWMIService = GetObject("winmgmts:" &  strComputer & "\root\cimv2")
Set colItems = objWMIService.InstancesOf("Win32_Process")
count = 0
For Each objItem In colItems
If objItem.Name = strFlashEXEFileName Then
count = count + 1
Exit For
End If
Next
msgbox count
If count = 0 Then
Dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.Run strFlashEXEFilePath, 3, False       ' no return from .run
Set oShell = Nothing
For Each objItem In colItems
If objItem.Name = strFlashEXEFileName Then MsgBox "running": Exit For
Next
Else

End If
Set objWMIService = Nothing
Set colItems = Nothing

5. ## Re: Check running program, start if not running VBscript.

Originally Posted by Theatre
Oh... why didn't i even thought about the restart command.
May be because that "command" does not exist?

6. ## Re: Check running program, start if not running VBscript.

Nah not a command, the script thing or whatever you guys call it... LOL.

7. ## Re: Check running program, start if not running VBscript.

Originally Posted by westconn1
i would assume the should be a \ between the file path and file name, but that would also mean the full file path to be C:\BarkStat\BarkStat\BarkStat.exe, which seems may be an extra folder depth

i rewrote some and put in some testing, the code, tested against some .exe file on my machine, runs right through without error and i am sure it should work correctly
you can remove the testing when you are sure it is doing as required

Code:
Dim objWMIService, colItems, objItem, strComputer, strFlashEXEFile
Dim count

strFlashEXEFileName = "BarkStat.exe"

strFlashEXEFilePath = "C:\BarkStat\BarkStat" & strFlashEXEFileName

strComputer = "."

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")   ' impersonation seems essential on w10, but probably not required on xp or other earlier versions
'Set objWMIService = GetObject("winmgmts:" &  strComputer & "\root\cimv2")
Set colItems = objWMIService.InstancesOf("Win32_Process")
count = 0
For Each objItem In colItems
If objItem.Name = strFlashEXEFileName Then
count = count + 1
Exit For
End If
Next
msgbox count
If count = 0 Then
Dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.Run strFlashEXEFilePath, 3, False       ' no return from .run
Set oShell = Nothing
For Each objItem In colItems
If objItem.Name = strFlashEXEFileName Then MsgBox "running": Exit For
Next
Else

End If
Set objWMIService = Nothing
Set colItems = Nothing
This also worked perfect, i had to try this aswell.
Thanks for your time, much appreciated!

Br

8. ## Re: Check running program, start if not running VBscript.

Originally Posted by Theatre
This also worked perfect, i had to try this aswell.
Thanks for your time, much appreciated!

Br
Strange thing is it worked for a couple of runs, then all of a sudden it says (count = 1) "already running" even thou its not running.
I've checked taskmanager and the proccess is not active there...

EDITED: Found that problem, doesn't belong to the script...

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•