Visual Basic: Check if a process is running in the user session

This week a customer had some problems with an application. The application was partly web based, uses an Office plugin and integration with the Windows Explorer.
Normally I would say that it shouldn’t be a problem.
But the nasty thing about this application was that the application was not supported within a multiuser environment.
The application checks if a specific process exists in the task list, and if so it continues with his tasks and doesn’t start the process for every single user.
Okay, starting that specific process at logon for every user could be a solution.
In my case that worked for a little while, because the process will stop after some idle time and then you’re in the same situation as we started with.
So I created a small Visual Basic script that will check if a process is running in the user session and if not it will start the process.
The script will check for the running process every 30 seconds.
Why Visual Basic and not in PowerShell scripting? Simple.. Because users running PowerShell scripts was not permitted in the customer’s environment, and wscript can run in the background.

'Created by Hendricus Kramer - www.pepperbyte.com

'Global variabeles
dim strProcessPath, strProcessName
dim intSleepTime

'Declare variabeles
strProcessPath = """C:\path\to\proc.ext"""
strProcessName = "proc.ext"
'30 seconds = 0.5x60x1000
intSleepTime = 0.5*60*1000


' ----------------------------------------------------------------------------------------------------------------------------------------------------------
' DO NOT CHANGE A BIT BENEATH THIS LINE
' ----------------------------------------------------------------------------------------------------------------------------------------------------------
'Start main
main

sub main
'Main program
                'Start loop
                Do
                                if checkProcess(strProcessName, getCurrentLoggedonUser) = true then
                                                'Process found, nothing to do
                                                '---------For debug uncomment the following line:
                                                'msgbox "Process found for user " & getCurrentLoggedonUser & ", no action needed."
                                else
                                                'Process not found, start the process
                                                '---------For debug uncomment the following line:
                                                'msgbox "Process not found for user " & getCurrentLoggedonUser & ", start the process."
                                                startProcess strProcessPath
                                end if
                                'Wait for x seconds to continue loop
                                WScript.Sleep intSleepTime
                Loop
end sub

function checkProcess(ProcessName, LoggedOnUser)
                'Check if the given process exists for the current logged on user
                'Returns true or false

                'Declare variables
                Dim objWMIService, colProcessList

                'Declare objects
                                Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
                                Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process WHERE Name LIKE '" & ProcessName & "%'")

                'Check if process exists
                                If colProcessList.count>0 then
                                                'The process exists, check if the process is running under the current logged on user
                                                For each objProcess in colProcessList
                                                                objProcess.getOwner strUser
                                                                if strUser = LoggedOnUser then
                                                                                'Process found for currentlogged on user
                                                                                'Return True
                                                                                checkProcess = true
                                                                end if
                                                next
                                else
                                                'Process does not exists, return false
                                                checkProcess = false
                                End if
                'Clean up objects
                                Set objWMIService = nothing
                                Set colProcessList = nothing
end function

function getCurrentLoggedonUser()
                'Get the username of the current logged on user
                'Returns the user name
                'Declare variable
                Dim objNetwork
                'Declare object
               Set objNetwork = CreateObject("WScript.Network")

                'Return the username
                getCurrentLoggedonUser = objNetwork.UserName

                'Clean up objects
                                Set objNetwork = nothing
end function

sub startProcess(path)
                'Start a process
                'Declare variable
                Dim objShell
                'Declare object
                Set objShell = createobject("Wscript.Shell")

                'Start the process
                objShell.Run path

                'Clean up objects
                Set objShell = nothing
end sub