Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to check services on a remote machine and if one service is running out of the bunch i need to display that no action can be taken and if all services are stopped then action can be taken.

I have the following code that our las guy made and i was thrown into his role even though i dont code. To me it looks like the loop is there but i dont understand how to work it. Please help.

VB
Dim check1 As String
Dim check2 As String
Dim check3 As String
Dim check4 As String
Dim check5 As String
Dim Check6 As String
Dim Check7 As String
Dim strProcess As String

Do
    strProcess = "Radskman.exe"
Loop Until strProcess <> ""
Do

Loop Until strComputer <> ""
If (IsProcessRunning(strComputer, strProcess) = True) Then
    check1 = "Running"
Else
    check1 = ""
End If

Do
    strProcess = "Hide.exe"
Loop Until strProcess <> ""
Do

Loop Until strComputer <> ""
If (IsProcessRunning(strComputer, strProcess) = True) Then
    check2 = "Running"
Else
    check2 = ""
End If

Do
    strProcess = "RadPinit.exe"
Loop Until strProcess <> ""
Do

Loop Until strComputer <> ""
If (IsProcessRunning(strComputer, strProcess) = True) Then
    check3 = "Running"
Else
    check3 = ""
End If

Do
    strProcess = "RadConnect.exe"
Loop Until strProcess <> ""
Do

Loop Until strComputer <> ""
If (IsProcessRunning(strComputer, strProcess) = True) Then
    check4 = "Running"
Else
    check4 = ""
End If

Do
    strProcess = "Connect.exe"
Loop Until strProcess <> ""
Do

Loop Until strComputer <> ""
If (IsProcessRunning(strComputer, strProcess) = True) Then
    check5 = "Running"
Else
    check5 = ""
End If

Do
    strProcess = "exbexit.exe"
Loop Until strProcess <> ""
Do

Loop Until strComputer <> ""
If (IsProcessRunning(strComputer, strProcess) = True) Then
    Check6 = "Running"
Else
    Check6 = ""
End If

Do
    strProcess = "Forcedreboot.exe"
Loop Until strProcess <> ""
Do

Loop Until strComputer <> ""
If (IsProcessRunning(strComputer, strProcess) = True) Then
    Check7 = "Running"
Else
    Check7 = ""
End If


If check1 = "" And check2 = "" And check3 = "" And check4 = "" And check5 = "" And Check6 = "" And Check7 = "" Then
    CAEState = " CAE is not Running"

ElseIf check1 = "Running" Or check2 = "Running" Or check3 = "Running" Or check4 = "Running" Or check5 = "Running" Or Check6 = "Running" Or Check7 = "Running" Then
    CAEState = " CAE is Running"
End If


missing function i found:

VB
Function IsProcessRunning(ByVal strServer, ByVal strProcess)
    Dim Process, strObject
    IsProcessRunning = False
    strObject = "winmgmts://" & strServer
    For Each Process In GetObject(strObject).InstancesOf("win32_process")
        If UCase(Process.name) = UCase(strProcess) Then
            IsProcessRunning = True
            Exit Function
        End If
    Next
End Function
Posted
Updated 24-Feb-12 4:33am

1 solution

There are so many things wrong with that code I don't even know where to begin. I hope the guy who wrote it is long gone.

The loops make no sense and serve no purpose, they can be ignored, I think they're a left over from when this code was actually written as a loop that worked across a list of processes.

Also, CAEState is set to " CAE is not Running" if all the processes are dead and it's set to " CAE is Running" if at least one process is running meaning that if one out of seven is up CAE is deemed to be up. This might or might not be correct. Seems weird to me.

I would suggest trying something more like this;

VB
Dim processes() As String = {"Radskman.exe", "Hide.exe", "RadPinit.exe", "RadConnect.exe", "Connect.exe", "exbexit.exe", "Forcedreboot.exe"}
Dim allAreRunning As Boolean = True
Dim atLeastOneIsRunning As Boolean = False

For Each processName In processes
    Dim isRunning As Boolean = IsProcessRunning(processName, "somecomputername")

    allAreRunning = allAreRunning And isRunning
    atLeastOneIsRunning = atLeastOneIsRunning Or isRunning
Next

If allAreRunning Then
    ' Do something when all are running
End If

If atLeastOneIsRunning Then
    ' do something when at least one is running
End If


Hope this helps,
Fredrik
 
Share this answer
 
Comments
Zachary.shupp 24-Feb-12 10:35am    
this looks great i just tested and it has issue but it showed me that there was a function for the item and i updated the question with the function.
When i run it i get in the function a activexx error
Fredrik Bornander 24-Feb-12 11:37am    
That's a separate issue that I can't comment on without seeing more code. I don't know how IsProcessRunning is implemented.
Fredrik Bornander 24-Feb-12 11:38am    
I'm assuming you replaced my "somecomputername" with the variable holding the computer name?
Zachary.shupp 24-Feb-12 11:39am    
yes i did

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900