Hi All
Do you know of any AHK code that would check if process, lets say abc.exe is running under given username?
Thank you.
Howt to check if process is running under given username? Topic is solved
Re: Howt to check if process is running under given username?
Code: Select all
Process, Exist, notepad.exe
msg := (Errorlevel != 0) ? "Process 'notepad' exist!" : "Process 'notepad' doesn't exist ATM"
MsgBox % "Dear " A_UserName "`n ..." msg
Re: Howt to check if process is running under given username?
Hallo,
try:
try:
Code: Select all
Process, Exist, abc.exe
IF ErrorLevel
{
UserName := GetProcessUser(ErrorLevel)
MsgBox,% "UserName: " UserName
}
Else
MsgBox, Process doesn't exist
Return
GetProcessUser(PID)
{
VarSetCapacity(UN, 24, 0)
UserName := ComObject(0x400C, &UN) ; combination of VT_BYREF and VT_VARIANT.
For Process in ComObjGet("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where ProcessId = '" . PID . "'")
Process.GetOwner(UserName)
Return UserName[]
}
Re: Howt to check if process is running under given username?
Thank you for you reply.BoBo wrote: ↑08 Aug 2020, 04:14Code: Select all
Process, Exist, notepad.exe msg := (Errorlevel != 0) ? "Process 'notepad' exist!" : "Process 'notepad' doesn't exist ATM" MsgBox % "Dear " A_UserName "`n ..." msg
Your code checks process under user where you execute it. I have multiple users logged in (Windows Server). What i am trying to achieve is to detect every few minutes if process is still running under user1, user2 etc. and if not perform an action...
Re: Howt to check if process is running under given username?
Your code is interesting. It gives msgbox that abc.exe is running by user2 while it is running under 3 other users also. Do you know to transform that code so it would work like "if user1 is running abc.exe then do action", "if user2 is running abc.exe then do action"?Rohwedder wrote: ↑08 Aug 2020, 05:00Hallo,
try:Code: Select all
Process, Exist, abc.exe IF ErrorLevel { UserName := GetProcessUser(ErrorLevel) MsgBox,% "UserName: " UserName } Else MsgBox, Process doesn't exist Return GetProcessUser(PID) { VarSetCapacity(UN, 24, 0) UserName := ComObject(0x400C, &UN) ; combination of VT_BYREF and VT_VARIANT. For Process in ComObjGet("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where ProcessId = '" . PID . "'") Process.GetOwner(UserName) Return UserName[] }
Thank you.
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Howt to check if process is running under given username?
I get the idea that this is an administrative task that should be handled using Powershell ??
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Howt to check if process is running under given username?
Code: Select all
#Persistent
exeName := "notepad.exe"
userFunc := "MyFunc"
WMI := ComObjGet("winmgmts:")
sink := ComObjCreate("WbemScripting.SWbemSink")
ComObjConnect(sink, new EventSink(Func(userFunc)))
WMI.ExecNotificationQueryAsync(sink, "select * from __InstanceCreationEvent Within 1 "
. "Where TargetInstance ISA 'Win32_Process' "
. "and TargetInstance.Name = '" . exeName . "'")
class EventSink
{
__New(userFunc) {
this.userFunc := userFunc
}
OnObjectReady(obj) {
static type := (VT_VARIANT := 0xC) | (VT_BYREF := 0x4000)
Process := obj.TargetInstance
VarSetCapacity(variant, 24, 0)
User := ComObject(type, &variant)
Process.GetOwner(User)
timer := this.userFunc.Bind(Process.ProcessID, User[])
SetTimer, % timer, -10
}
}
MyFunc(PID, owner) {
MsgBox, % "PID: " . PID . "`nOwner: " . owner
}
Re: Howt to check if process is running under given username? Topic is solved
Sorry, but there are almost no MSDN tutorials in my mother tongue.
Code: Select all
MsgBox,% Running("abc.exe","user2")
Running(exeName,User)
{ ;1:yes 0:no
For Proc in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
IF (Proc.Name = exeName)
IF (User = GetProcessUser(Proc.ProcessId))
Return, True
Return, False
}
GetProcessUser(PID)
{
VarSetCapacity(UN, 24, 0)
UserName := ComObject(0x400C, &UN) ; combination of VT_BYREF and VT_VARIANT.
For Proc in ComObjGet("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where ProcessId = '" . PID . "'")
Proc.GetOwner(UserName)
Return UserName[]
}
Re: Howt to check if process is running under given username?
This is awesome! Works as it should.Rohwedder wrote: ↑08 Aug 2020, 09:54Sorry, but there are almost no MSDN tutorials in my mother tongue.Code: Select all
MsgBox,% Running("abc.exe","user2") Running(exeName,User) { ;1:yes 0:no For Proc in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process") IF (Proc.Name = exeName) IF (User = GetProcessUser(Proc.ProcessId)) Return, True Return, False } GetProcessUser(PID) { VarSetCapacity(UN, 24, 0) UserName := ComObject(0x400C, &UN) ; combination of VT_BYREF and VT_VARIANT. For Proc in ComObjGet("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where ProcessId = '" . PID . "'") Proc.GetOwner(UserName) Return UserName[] }
Could anyone help me with transforming that code to actually perform my lines of code instead of displaying message box?
Let's say i would like to run c:\yes.exe for yes and c:\no.exe for no...
Thank's a lot for all replies here!
Re: Howt to check if process is running under given username?
Then replace MsgBox,% Running("abc.exe","user2") by:
Code: Select all
IF Running("abc.exe","user2")
Run, c:\yes.exe
Else
Run, c:\no.exe
Re: Howt to check if process is running under given username?
Awesome! Many thanks for your help!Rohwedder wrote: ↑09 Aug 2020, 00:26Then replace MsgBox,% Running("abc.exe","user2") by:Code: Select all
IF Running("abc.exe","user2") Run, c:\yes.exe Else Run, c:\no.exe