Results 1 to 13 of 13

Thread: Check running program, start if not running VBscript.

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    7

    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
    Last edited by Theatre; Mar 8th, 2021 at 01:13 AM.

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,094

    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. #3

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    7

    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. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
        MsgBox "already running"
    
    End If
    Set objWMIService = Nothing
    Set colItems = Nothing
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,987

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

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

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    7

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

    Nah not a command, the script thing or whatever you guys call it... LOL.
    Last edited by Theatre; Jan 7th, 2021 at 01:46 AM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    7

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

    Quote Originally Posted by westconn1 View Post
    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
        MsgBox "already running"
    
    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. #8

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    7

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

    Quote Originally Posted by Theatre View Post
    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...
    Last edited by Theatre; Jan 7th, 2021 at 06:26 AM.

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    7

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

    Hello!

    Im resuming this thread because I've got a new problem.

    It has been working for some months now but all of a sudden this happend:
    Attachment 180420

    https://imgur.com/MNPCMVo

    Anyone who has any idea, it doesnt work at all now.
    I haven't done anything with code, its the same as above.

    Br
    Theatre
    Last edited by Theatre; Mar 5th, 2021 at 06:04 AM.

  10. #10
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,223

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

    Try the attachment again, this time on imgur, this forum occasionally and regularly eats attachments

  11. #11

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    7

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

    https://imgur.com/MNPCMVo

    https://imgur.com/2qvITE8

    For some unknown reason the Line is now 28 instead of 23 as the picture describes.
    Still havent change a thing in the code.
    Last edited by Theatre; Mar 8th, 2021 at 01:07 AM.

  12. #12
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,801

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

    I noticed a recommendation to dim all variables. You can use "Option Explicit" at the top of your code to make vbscript enforce this rule for you.

  13. #13
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,223

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

    The two lines mentioned are where variables are used, that have not been previously declared.

Posting Permissions

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



Click Here to Expand Forum to Full Width