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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect if exe is running advanced

Status
Not open for further replies.

danmat46

Programmer
Nov 13, 2007
23
GB
Hello!
I have a scheduled task running under user b with the server logged on as user a.
Problem is I want to detect from this scheduled task if an exe is running on the server by user a

Any ideas or have I lost you all?

Thank you
 
Replace the parts in red with the process name and user you wish to look for. Replace the blue text with a remote machine name or leave as is if you wish to query the local machine.

Code:
strComputer = "[blue].[/blue]"
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Process")
For Each objProcess in colProcessList
 colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)
 If objProcess.Name = "[red]notepad.exe[/red]" And strNameOfUser = "[red]markmac[/red]" Then
 	WScript.Echo "Process " & objProcess.Name & " is running under user: " _
 	& strUserDomain & "\" & strNameOfUser & "."
 End If
Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Hello, I know this thread is a little old but it's almost what I need; I was hoping someone could help me take a similar script a step further.

What I want to accomplish: (on a Terminal Server) Grab all user processes named psi.exe and grab the owner of the process. Then match the user who ran the script to the psi.exe process (if it's running) If psi.exe owned by x user exists then launch an application; If psi.exe does not exist for x user then start psi.exe and then launch application.

I think I am pretty close, this script will do what I want, but it launches psi.exe as many times as it finds the process listed because of the For Each loop. I can't figure out how to narrow down my data to just that one instance, or lack there of. Any assistance would be greatly appreciated! below is my code so far...

Code:
strComputer = "."

Dim WSHShell, WSHNetwork, objDomain, DomainString, strNameOfUser, UserString, UserObj, objWMIService, ColProcessList, objProcess

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")

DomainString = Wshnetwork.UserDomain 
UserString = WSHNetwork.UserName
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

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

Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process" & _
     " WHERE Name = 'psi.exe'")

For Each objProcess in colProcessList
    colProperties = objProcess.GetOwner( _
        strNameOfUser,strUserDomain)

    If strNameOfUser = UserString Then
	WshShell.Run "c:\Intergy\IntergyStart.bat"
	'Wscript.Echo "Process: " & objProcess.Name _
        '& " is owned by " _ 
        '& strNameOfUser & " and is already running"
    Else
	WshShell.Run "\\172.16.1.201\profiles\Desktop\Psi.lnk"
    End If
Next

set WSHShell = nothing
set WSHNetwork = nothing
set UserObj = nothing
set colProcessList = nothing
set strNameOfUser = nothing
set UserString = nothing
set Userobj = nothing
set objWMIService = nothing
set DomainString = nothing
 
Here are the relevant bits you need:

Code:
Set WSHNetwork = CreateObject("Wscript.Network")
UserString = WSHNetwork.UserName

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Process WHERE name = 'psi.exe'")
For Each objProcess in colProcessList
 colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)
 If strNameOfUser = UserString Then 
 	WScript.Echo "Process " & objProcess.Name & " is owned by " _
 	& strUserDomain & "\" & strNameOfUser & "."
 End IF
Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thank you Mark for your swift response, the code you posted is similar to something I started with. The problem is it only acts upon finding a psi.exe process owned by the specific user; I also want the script to act if it does not find a process called psi.exe owned by the specific user, but not act upon finding other psi.exe process owned by other users. (sorry to be confusing :D )

I think my goal could be accomplished if I could run a search string through the colProperties after grabbing the owners, filtering my data down to either nothing or the one psi.exe process owned by the specific user; then I could use IF statements to perform the desired actions. The logic would look like this:

Connect to Computer "."
> Grab all Processes named psi.exe
> Grab all owners of psi.exe
> Grab only psi.exe owned by UserString (WSHnetwork.UerName)
> If exists perform action 1, then quit
> If not exists perform action 2 and then action 1, then quit

I am starting to wonder if this is even possible with vbscript I can't find any exmaples close to it on the internet. Thank you again for your assistance and let me know if I confused the matter.

Cheerz!
Matt

 
Code:
Set WSHNetwork = CreateObject("Wscript.Network")
UserString = WSHNetwork.UserName

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Process WHERE name = 'psi.exe'")
For Each objProcess in colProcessList
 colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)
 If strNameOfUser = UserString Then 
     'Do nothing
 Else
    WScript.Echo "Process " & objProcess.Name & " is owned by " _
     & strUserDomain & "\" & strNameOfUser & "."
 End IF
Next

Sent while on vacation in Cozumel....

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thank you again Mark for your assistance, I was just in Cozumel / Playa Del Carmen a few months ago. I hope your having an awesome vacation!

I finally got the script to function as desired, I will post it for others should they need it.

Code:
Dim WSHShell, WSHNetwork, objDomain, strNameOfUser, UserString, objWMIService, ColProcessList, objProcess, PsiFlag

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
UserString = WSHNetwork.UserName

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Process WHERE name = 'psi.exe'")
For Each objProcess in colProcessList
 colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)

 If strNameOfUser <> UserString Then 
     PsiFlag = 0
     'WScript.Echo "Psi owned by " & strNameOfUser & " NOT " & UserString & "." 'Test for Returned Data
     END IF
 If strNameOfUser = UserString Then 
     WshShell.Run "c:\Intergy\IntergyStart.bat"
     PsiFlag = 1
     'WScript.Echo "Process " & objProcess.Name & " is owned by " _  'Test for Returned Data
     '& strNameOfUser & " Which matches " & UserString & "."
     END IF
 If IsEmpty(strNameOfUser) Or IsNull(strNameOfUser) Then 
     PsiFlag = 0
     END IF
Next

If PsiFlag = 0 Then 
     WshShell.Run "\\172.16.1.201\profiles\Desktop\Psi.lnk" 
     WshShell.Run "c:\Intergy\IntergyStart.bat"
     'WScript.Echo "Psi Needs to be started by User" 'Test for Returned Data
     END IF

set WSHShell = nothing
set WSHNetwork = nothing
set UserObj = nothing
set colProcessList = nothing
set strNameOfUser = nothing
set UserString = nothing
set Userobj = nothing
set objWMIService = nothing
set DomainString = nothing
set PsiFlag = nothing

If anyone knows of a "cleaner" way to accomplish this I would love to know :) .

There were multiple problems with my original code:

1.) This script is going to be run by Terminal server users with basic user permissions. When attaching to Win32_Process and grabbing the Owners, Empty or Null data was being returned because the user doesn't have permission to view other users processes; calling the strNameOfUser variable would echo nothing.

2.) I can't find any information on further filtering the data so I had to think of another way to turn the results into a true or false statement. I decided to set a variable to 0 if the conditions were false and 1 if they were true; false = Process owner not equal to logged in user & Process owner is empty or null, True = owner of process is equal to logged on user.

I am not sure why this works, you would think the variable would overwrite itself as the For Each loop runs and the end result would always be a 0 (false); unless the process owned by the logged in user is always last, but I tested it a hundred times and it works as expected...

Anywho, thank you Mark for your assistance and have a great Vacation!

Cheerz!
Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top