Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

Process Killer

$
0
0
Good Afternoon,

I'm looking for someone that could possibly answer a few questions for me related to the remote process killer I've been working on.  The reason for the process killer is because I work in a corporate level domain and there are some instances where we have machines locked down to the point that we disallow the usage of Task Manager, but we have software that occassionally needs to be killed.  With that said, the current, entire script is below, and is mostly functional, but I have a few questions.

1)  The line beginning with "ElseIf $ProcessToKill" seems to not allow any process to be killed, rather than only those listed in the Or statements.  How can I rewrite this line so that it actually functions as intended?

2) If I click the remove button on the script when nothing is selected, the script will pick the first item and remove it.  Is there any way to set the focus of the listview so that nothing is selected when the script first starts?  I attempted to do so with "_GUICtrlListView_SetItemFocused($CompListView, -1)" but that didn't appear to work.

3) Finally, I need to figure out a way to remove duplicate items from the ListView, or at least ignore them and list only one of the duplicates.  I was thinking the best way to do this may be to use FileReadToArray and have that populate the ListView rather than reading the file directly into the ListView itself.  So any pointers on getting me down that path would be beneficial.

Additional Files required for the script:  Computers.txt and Processes.txt (First contains a list of pc's, second a list of processes to populate the combobox with)

[ autoit ]         
#include <GUIConstantsEx.au3> #include <GuiComboBox.au3> #include <GuiListView.au3> #include <WindowsConstants.au3> #include <file.au3> Global $Add, $Form1, $Form2, $EndProcess, $ProcessToKill, $Process, $SelectedComp, $CompListView, $ipAddress, $index, $aRecords, $Remove, $OK, $CompName, $file, $CompNameInput, $Text, $i3, $CountFile3, $hFile Opt("GUIOnEventMode", 1) Main()   Func Main()     Local $hFile = FileOpen("Computers.txt")     $Form1 = GUICreate("Computers", 331, 251, 192, 124)  GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close")     GUISetState(@SW_SHOW)  $Process = GUICtrlCreateCombo("", 200, 48, 121, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))     $CompListView = GUICtrlCreateListView("Computers", 8, 48, 169, 162)  GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 165)     $Add = GUICtrlCreateButton("Add", 8, 216, 81, 25)     GUICtrlSetOnEvent(-1, "On_Button")     $Remove = GUICtrlCreateButton("Remove", 96, 216, 81, 25)     GUICtrlSetOnEvent(-1, "On_Button")     $EndProcess = GUICtrlCreateButton("End Process", 200, 80, 121, 25)     GUICtrlSetOnEvent(-1, "On_Button")     popComboBox()  While 1         $Text = FileReadLine($hFile)         If @error = -1 Then Exitloop         GUICtrlCreateListViewItem($Text, $CompListView)     WEnd   While 1    Sleep(1000)   WEnd   EndFunc   ;==>Main   Func addComputer()    $Form2 = GUICreate("Add", 194, 88, 190, 255)    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close")    $CompName = GUICtrlCreateInput("", 8, 32, 169, 21)    $LabelComp = GUICtrlCreateLabel("Computer Name:", 8, 8, 83, 17)    $OK = GUICtrlCreateButton("OK", 104, 56, 73, 25)    GUICtrlSetOnEvent(-1, "On_Button")    GUISetState(@SW_SHOW)   EndFunc ; Func addComputer() Func On_Button()    Switch @GUI_CTRLID ; See which button sent the message    Case $Add    addComputer()      Case $EndProcess    TCPStartup()    $ProcessToKill = GUICtrlRead($Process)    Local $index = Int(_GUICtrlListView_GetSelectedIndices($CompListview))    $SelectedComp = _GUICtrlListView_GetItemTextString($CompListView, $index)    $ipAddress = TCPNameToIP($SelectedComp)    If @error Then    MsgBox(4112, "Error", "An unknown error has occurred.  Please try again.")    Return    ElseIf $ipAddress = "" Then    MsgBox(4112, "Error", "Couldn't Resolve IP Address.  Please check that the computer is on and has network connectivity.")    Return    ElseIf $ProcessToKill = "explorer.exe" Or "svchost.exe" Or "winlogon.exe" Or "crss.exe" Or "services.exe" Or "lsass.exe" Or "dllhost.exe" Or "spoolsv.exe" Or "regsvc.exe" Or "smss.exe" Or "alg.exe" Or "wscntfy.exe" Or "dwm.exe" Or "wininit.exe" Or "msascui.exe" Or "slsvc.exe" Or "taskhost.exe" Or "lsm.exe" Or "sppsvc.exe" Then    ConsoleWrite($ProcessToKill)    MsgBox(4112, "Error", "That is a protected process which may not be terminated.")    Return    Else    EndProc()    MsgBox(48, "Success", "The selected process has been terminated.")    TCPShutdown()    EndIf    Case $Remove          delSelected()    Case $OK       addToFile()    EndSwitch EndFunc ;On_Button() Func On_Close()    Switch @GUI_WINHANDLE ; See which GUI sent the CLOSE message    Case $Form1       Exit ; If it was this GUI - we exit <<<<<<<<<<<<<<<    Case $Form2       GUIDelete($Form2) ; If it was this GUI - we just delete the GUI <<<<<<<<<<<<<<<       GUICtrlSetState($Add, $GUI_ENABLE)    EndSwitch EndFunc ; Func On_Close() Func popComboBox()    $Countfile2= _FileCountLines("processes.txt")    $file2 = "processes.txt"    For $i2 = 1 To $CountFile2    $var2= FileReadLine($file2, $i2)    Global $text2 = $var2    _GUICtrlComboBox_AddString($Process, $text2)    Next EndFunc Func EndProc()  $oWMIService = ObjGet("winmgmts:\\" & $ipAddress & "\root\CIMV2")  If Not IsObj($oWMIService) Then   MsgBox(48, "ERROR", "Couldn't locate the computer. Please make sure you've selected the correct computer and try again.")   Return  EndIf  Dim $handle, $colProc  $cProc = $oWMIService.ExecQuery('SELECT * FROM Win32_Process WHERE Name = "' & $ProcessToKill & '"')   For $oProc In $cProc   $oProc.Terminate()    Next  If $handle Then   Return $handle  Else   Return 0  EndIf  EndFunc ; Func EndProc() Func delSelected()    Dim $aRecords       _FileReadToArray("computers.txt",$aRecords)    Local $index = Int(_GUICtrlListView_GetSelectedIndices($CompListview))    $SelectedComp = _GUICtrlListView_GetItemTextString($CompListView, $index)    For $x = 1 to $aRecords[0]    If stringinstr($aRecords[$x], $SelectedComp) then _FileWriteToLine("computers.txt", $x, "", 1)    _GUICtrlListView_DeleteAllItems($CompListView)    Local $hFile = FileOpen("Computers.txt")    While 1         $Text = FileReadLine($hFile)         If @error = -1 Then Exitloop      GUICtrlCreateListViewItem($Text, $CompListView)       WEnd      _GUICtrlListView_SetItemFocused($CompListView, -1)    Next EndFunc ; delSelected() Func addToFile()    FileWriteLine("Computers.txt", GUICtrlRead($CompName))    GUIDelete($Form2) ; If it was this GUI - we just delete the GUI <<<<<<<<<<<<<<<    GUICtrlSetState($Add, $GUI_ENABLE)        _GUICtrlListView_DeleteAllItems($CompListView)    Local $hFile = FileOpen("Computers.txt")    While 1         $Text = FileReadLine($hFile)         If @error = -1 Then Exitloop         GUICtrlCreateListViewItem($Text, $CompListView)       WEnd      _GUICtrlListView_SetItemFocused($CompListView, -1) EndFunc ; addToFile()

Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>