Jump to content

Detect and close process in shared desktop


 Share

Recommended Posts

Hi All
I recently starting using AutoIT script and this is my first ever post in AutoIT forum and I am hoping I am posting in the correct place.
I am trying to find a process name and if present kill that process in a Shared Hosted Environment (e.g. Citrix Published Desktop - Server 2019) for the currently logged on user.

So I started with simple code. On Server 2019 there are 3 users logged on (user1, user2, user3) and all 3 users have a process (let's assume) "xyz.exe" that I am trying to detect (only for currently logged on user). The script is actually run by the currently logged on user.


If ProcessExists("xyz.exe") Then
    MsgBox($MB_SYSTEMMODAL, "Process XYZ", "Process XYZ exists.")
EndIf

So when I ask user1 to run this script while xyz.exe is running, user1 gets the message box. Then I ask user to manually kill the process from Task Manager and run the script again and it still prompts the message that xyz exists. I am assuming that since user2 and user3 also have the xyz.exe process running the script it is detecting those processes and returning true.

So question is what is the correct way that script only detects the process for the user that runs the script (currently logged on - e.g. user1) and how do I kill the process only for that user (user1)?

I also tried the below to detect the process for user1 only but no luck

Local $aList = WinList()
Local $Title
Local $Found = 0
For $i = 1 To $aList[0][0]
    If $aList[$i][0] <> "" And BitAND(WinGetState($aList[$i][1]), 2) Then
        $Title = $aList[$i][0]
        $Found = StringInStr($Title, "XYZ")
        If $Found <> 0 Then
            MsgBox($MB_ICONWARNING, "Process XYZ", "Process XYZ exists.")
            ExitLoop
        EndIf
    EndIf
Next

Any help is appreciated!
thanks

Link to comment
Share on other sites

  • Developers

Here is some snippet you could use for this:

#include <MsgBoxConstants.au3>
#include <WinAPIProc.au3>
$Program = "xyz.exe"

$pid = ProcessExists($Program)

If $pid Then ; Check if the Notepad process is running.
    ; Get process UserName owning the process
    $Owner = "Unknown"
    $aOwner = _WinAPI_GetProcessUser($pid)
    If $aOwner <> 0 Then $Owner = $aOwner[0] ; $aOwner = 0 when no access to that information
    ; Check if current user owns it
    If $Owner = @UserName Then
        MsgBox($MB_SYSTEMMODAL, "", $Program & " is running for this user.")
    Else
        MsgBox($MB_SYSTEMMODAL, "", $Program & " is running for another user:" & $Owner)
    EndIf
Else
    MsgBox($MB_SYSTEMMODAL, "", $Program & " is not running.")
EndIf

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks jos for the snippet. I tried it but I get "xyz.exe is running for another user:Unknown" message......even though the process is running under user1's session. I closed the process manually, ran the script again and I got the same message which is expected....then I launched the process and ran the same script and same message (which is not expected).

thanks

Link to comment
Share on other sites

  • Developers

I ttested it on my Laptop ok, but not with multiple occurrences of the program.
Only thing I can think of is that you need to get the all occurrences of that program with ProcessList() and loop through the returned array testing whether any of these pids have as process owner the current user.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Just had a quick and dirty go at it so maybe this works for you:  ;) 

#include <MsgBoxConstants.au3>
#include <WinAPIProc.au3>
$Program = "xyz.exe"

$Status = "Not Running"
$aProcessList = ProcessList($Program)
If $aProcessList[0][0] > 0 Then
    $Status = $Program & " is Running " & $aProcessList[0][0] & " time(s)"
    $SUser = "for another user(s)"
    $Owner = "Unknown"
    For $i = 1 To $aProcessList[0][0]
        $aOwner = _WinAPI_GetProcessUser($aProcessList[$i][1])
        If $aOwner <> 0 Then $Owner = $aOwner[0]
        If $Owner = @UserName Then
            $SUser = "and atleast once for this user"
            ExitLoop
        EndIf
    Next
EndIf

MsgBox($MB_SYSTEMMODAL, "", $Status & " " & $SUser)

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...