Jump to content

Script only kills logged in user's instance instead of all running instances


Recommended Posts

Hi!  My script below is supposed to kill all instances of program.exe running (I have a desktop .exe that users click to run it).  The program hogs a serial port and people forget to log out, causing issues for the next user who tries to use the program.   However, even when I run it while logged in as the local administrator account, it ONLY closes the logged-in user's instance and no one else's.  This is on Windows 10 Professional (latest service pack, etc).  Any ideas?

 

$struusername = "administrator"
$strpassword = "password"
$strdomain = @ComputerName
$systemdrive = EnvGet("SYSTEMDRIVE")
$process = "program.exe"
$processname = "ProgramName"
$return = MsgBox(4, "Stop " & $processname, "Would you like to stop " & $processname & " ?")
If $return == 6 Then
    RunAsWait($struusername, $strdomain, $strpassword, 0, "cmd.exe /c taskkill /F /IM " & $process)
EndIf

Link to post
Share on other sites

You are running cmd.exe as admin, but taskkill is still running in the context of the logged in user? 

Things to try to test that:

Specify the username and password in your taskkill command instead of the runas for cmd.exe (https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill

Or #requireadmin might get you there as well, if changing the user context of the script changes the behavior of taskkill, thats a blind stab though.

 

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to post
Share on other sites

You can use something like the following function to list the users for a particular process and then kill the appropriate process id, 

#RequireAdmin

#include <Array.au3>

Global $g_oComError = ObjEvent("AutoIt.Error","MyErrFunc")

;~ Kill all $g_sProcessName that DON'T belong to $g_ProcessUser
;~ Use: ScriptName.exe Username to define the username you don't want closed (default @Username)
Global $g_ProcessUser = $CmdLine[0] > 0 ? $CmdLineRaw : @UserName
Global $g_sProcessName = "Scite.exe"
Global $g_aProcessUsers = _GetProcessOwners("scite.exe")

_ArrayDisplay($g_aProcessUsers)

For $i = UBound($g_aProcessUsers) - 1 To 0 Step - 1
    If $g_aProcessUsers[$i][1] <> $g_ProcessUser Then
        MsgBox(4096, "Process Info", "ProcessClose(" & $g_aProcessUsers[$i][2] & ") or TaskKill the following: " & @CRLF & _
                    "Process Name: " & $g_aProcessUsers[$i][0] & @CRLF & _
                    "Process UserName: " & $g_aProcessUsers[$i][1] & @CRLF & _
                    "Process Id " & $g_aProcessUsers[$i][2])
    EndIf
Next

Func _GetProcessOwners($_sProcessName, $_sComputerName = ".")
    Local $aProcessUsers[0][3]
    Local $oWMIService = ObjGet("winmgmts://" & $_sComputerName & "/root/cimv2")
    Local $oProcesses = $oWMIService.ExecQuery("Select * from Win32_Process Where Name ='" & $_sProcessName & "'")
    For $oProcess In $oProcesses
        If $oProcess.Name = $_sProcessName Then
            ReDim $aProcessUsers[UBound($aProcessUsers) + 1][3]
            $aProcessUsers[UBound($aProcessUsers) - 1][0] = $oProcess.Name
            $aProcessUsers[UBound($aProcessUsers) - 1][1] = Null
            $oProcess.GetOwner($aProcessUsers[UBound($aProcessUsers) - 1][1])
            $aProcessUsers[UBound($aProcessUsers) - 1][2] = $oProcess.ProcessId
        EndIf
    Next
    Return $aProcessUsers
EndFunc

Func _ObjErrorFunc()
    Local $iHexNumber = Hex($g_oComError.number,8)
    Msgbox(4096, "","We intercepted a COM Error !" & @CRLF & _
                    "Number is: " & $iHexNumber & @CRLF & _
                    "WinDescription is: " & $g_oComError.windescription & @CRLF & _
                    "Source is: " & $g_oComError.source & @CRLF & _
                    "Script Line is: " & $g_oComError.scriptline)
    Return ""
Endfunc

 

Edited by Subz
Link to post
Share on other sites

Ahhh, I was hoping Subz's was a little "works out of the box" haha and I'd just plop in my process names.  It's little too advanced for me, I'm afraid -- I have no idea what the array.au3 and cmdline.au3 files are supposed to contain, so it errors out immediately.    

Link to post
Share on other sites
On 12/31/2019 at 9:08 AM, Egg said:

However, even when I run it while logged in as the local administrator account, it ONLY closes the logged-in user's instance and no one else's.

Maybe just create a task scheduler event when user logged off that kills the process ?

Like always if you make a runable script with a specific path of actions so we can replicate the issue.  You would get more solution....

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...