Jump to content

Remote Process Terminator - Please help!


Cravin
 Share

Recommended Posts

Hello folks,

I'm working on a simple AutoIT script that uses VB script/WMIC to kill a remote process. I have the GUI designed, but need a little help getting everything working together.

On the Main GUI, I have a listbox that I'd like to be able to store and retreive data to using a CSV file. I have an Add and Remove button. The Add button pulls up form 2(see code below) which allows the entry of a Computer name and its IP.

Of course also on the main gui is the location to type in the name of the process, with a button that says End process which I would like to execute the script below when pressed. Anybody out there willing to guide me along in finishing this out?

Also, I should add, on the main gui... I don't want to show the IP address of the PC in the listbox, but when the script executes, I want the IP address to be used instead of the netbios name, if that makes sense.

;This script will kill a specified process.
;Chey Harden 8.11.06

Dim $objWMIService, $objProcess, $colProcess
Dim $strComputer, $strProcessKill, $strInput
$strProcessKill = inputbox("Enter Process Name Here","Process Name")
$strProcessKill = "'" & $strProcessKill & "'"

; Input Box to get name of machine to run the process
Do
$strComputer = (InputBox(" ComputerName to Run Script","Computer Name"))
If $strComputer <> "" Then
$strInput = 1
EndIf
Until $strInput = 1


$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!" & $strComputer & "rootcimv2")

$colProcess = $objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & $strProcessKill )
For $objProcess in $colProcess
$objProcess.Terminate()
Next

;VA WScript.Quit

;Form 1
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:UsersCravinDesktopKodaFormsForm1.kxf
$mainForm = GUICreate("Process Terminator", 491, 301, 192, 124)
$compList = GUICtrlCreateList("", 24, 64, 209, 188)
$Label1 = GUICtrlCreateLabel("Select the PC to terminate the process on:", 32, 40, 203, 17)
$Process = GUICtrlCreateInput("", 256, 64, 209, 21)
$Label2 = GUICtrlCreateLabel("Enter name of process to terminate:", 256, 40, 171, 17)
$endProc = GUICtrlCreateButton("End Process", 256, 240, 211, 49)
$add = GUICtrlCreateButton("Add", 24, 264, 99, 25)
$rem = GUICtrlCreateButton("Remove", 136, 264, 99, 25)
$Label3 = GUICtrlCreateLabel("Process Terminator", 152, 8, 186, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd

;Form 2

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:UsersCravinDesktopKodaFormsForm2.kxf
$Form2 = GUICreate("Add Computer", 227, 121, 203, 347)
$Label1 = GUICtrlCreateLabel("Computer Name", 8, 8, 80, 17)
$CompNameAdd = GUICtrlCreateInput("", 8, 32, 209, 21)
$OK = GUICtrlCreateButton("OK", 144, 88, 75, 25)
$CompAddIP = GUICtrlCreateInput("", 8, 88, 121, 21)
$IP = GUICtrlCreateLabel("IP Address", 8, 64, 55, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
Edited by Cravin
Link to comment
Share on other sites

  • Moderators

Cravin,

And why do you want to kill processes on remote PCs? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sorry,

This is a project for work. We use a program that frequently stalls out (can't recall the name of the program, was just tasked with this today by our Systems Administrator) and the way our group policy is set, the users cant Ctrl Alt Del to shut down processes, so we need an easy solution to do this remotely so someone with little PC knowledge can utilize it.

Link to comment
Share on other sites

Okay, I've done a bit more and this is where I'm at. I added the main gui in a function named Main() and the second gui (when Add is clicked) to a function named addGui(). I also added the VBscript that will terminate the specified process to its own function named killProc().

Biggest things I'd request help with are as follows:

1) Setting up the listbox in Main() to read data from a CSV file which contains 2 variables per line. A) Computer Name (netbios) and B) IP Address

2) Getting the End Process button on the main form to execute the killProc() function on the selected text in the listbox, so that it executes the script using the IP address instead of the computer name.

Anyone willing to help?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $buttonclicked=0;
Main()
Func Main()
#Region ### START Koda GUI section ###
$Form1_1 = GUICreate("Process Terminator", 491, 301, 192, 124)
$List1 = GUICtrlCreateList("", 24, 64, 209, 188)
$Label1 = GUICtrlCreateLabel("Select the PC to terminate the process on:", 32, 40, 203, 17)
$Process = GUICtrlCreateInput("", 256, 64, 209, 21)
$Label2 = GUICtrlCreateLabel("Enter name of process to terminate:", 256, 40, 171, 17)
$Button1 = GUICtrlCreateButton("End Process", 256, 240, 211, 49)
$Button2 = GUICtrlCreateButton("Add", 24, 264, 99, 25)
$Button3 = GUICtrlCreateButton("Remove", 136, 264, 99, 25)
$Label3 = GUICtrlCreateLabel("Process Terminator", 152, 8, 186, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
Case $Button2
 If $buttonclicked=0 Then
  $buttonclicked=1
 ElseIf $buttonclicked=1 Then
  $buttonclicked=0
  EndIf
 EndSwitch
 If $buttonclicked=1 Then
  addGui()
  EndIf
WEnd
EndFunc
Func addGui()
 #Region ### START Koda GUI section ###
$Form2 = GUICreate("Add Computer", 227, 121, 212, 320)
$Label1 = GUICtrlCreateLabel("Computer Name", 8, 8, 80, 17)
$CompNameAdd = GUICtrlCreateInput("", 8, 32, 209, 21)
$OK = GUICtrlCreateButton("OK", 144, 88, 75, 25)
$CompAddIP = GUICtrlCreateInput("", 8, 88, 121, 21)
$IP = GUICtrlCreateLabel("IP Address", 8, 64, 55, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
 EndSwitch
WEnd
EndFunc
Func killProc()
 ;This script will kill a specified process.
;Chey Harden 8.11.06
Dim $objWMIService, $objProcess, $colProcess
Dim $strComputer, $strProcessKill, $strInput
$strProcessKill = "" ; fix this variable to equal name of process to kill
$strProcessKill = "'" & $strProcessKill & "'"
; Input Box to get name of machine to run the process
Do
$strComputer = "";fix this variable *should use IP of selected computer from listbox*
If $strComputer <> "" Then
$strInput = 1
EndIf
Until $strInput = 1

$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colProcess = $objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & $strProcessKill )
For $objProcess in $colProcess
$objProcess.Terminate()
Next
EndFunc
;VA WScript.Quit
Link to comment
Share on other sites

The reason why I'm opposed to using PSTools is due to security concerns. When you pass credentials using PSTools, they are sent across the network via plain text, and we just can't have that. Also, a nice GUI interface will keep it user friendly, as the persons who will primarily use this will have little knowledge about computers.

*Edit* Well, the users have some knowledge but not enough to make me feel comfortable letting them use anything command line based.

Edited by Cravin
Link to comment
Share on other sites

I'm making progress on this.. running into this issue though.. In the following while loop in this function, how do I get it to stop running at the end of the file without closing the script completely? Currently it just cycles through over and over again.

*EDIT* Sorry, to be more specific, my text file currently contains "Computer1, 192.168.1.100", and this text is continuously being added to itself in the listbox, so the second line reads "Computer1, 192.168.1.100Computer1, 192.168.1.100" and so forth, where as I just want it to stop after reading the end of the file and in this case only displaying the one line once, if that makes sense.

Func gui1()
$Form1_1 = GUICreate("Process Terminator", 491, 301, 192, 124)
$List1 = GUICtrlCreateList("", 24, 64, 209, 188)
$Label1 = GUICtrlCreateLabel("Select the PC to terminate the process on:", 32, 40, 203, 17)
$Process = GUICtrlCreateInput("", 256, 64, 209, 21)
$Label2 = GUICtrlCreateLabel("Enter name of process to terminate:", 256, 40, 171, 17)
$Button1 = GUICtrlCreateButton("End Process", 256, 240, 211, 49)
$Button2 = GUICtrlCreateButton("Add", 24, 264, 99, 25)
GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
$Button3 = GUICtrlCreateButton("Remove", 136, 264, 99, 25)
$Label3 = GUICtrlCreateLabel("Process Terminator", 152, 8, 186, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function
     While 1
$Countfile= _FileCountLines("computers.txt")
$file = "computers.txt"
For $i = 1 To $CountFile
$var1= FileReadLine($file, $i)
$text = $text & @CRLF & $var1
_GUICtrlListBox_AddString($List1, $text)
Next
     WEnd
EndFunc ;==>gui1
Edited by Cravin
Link to comment
Share on other sites

Firstly, take this out of your while loop

$Countfile= _FileCountLines("computers.txt")
$file = "computers.txt"
For $i = 1 To $CountFile
$var1= FileReadLine($file, $i)
$text = $text & @CRLF & $var1
_GUICtrlListBox_AddString($List1, $text)
Next

And add it to a function of its own, and called via the press of a button.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

JohnOne,

Done, and thanks for the advice. I just realized why it was doing that over and over again, obviously, because it was in a loop! I feel silly. Anyway, I created a function called poplistbox and call it when then "Add" button is clicked (How would I get this to happen on startup of the script, also, to populate the list box from the txt file?) , and that is working, but each time I do it, it's now adding the text in the file to the list box over and over again, which I don't want. It seems like we need to create some way to do a check for duplicates and to have AutoIT discard them. For the sake of ease, here is the current code as it sits:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListview.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Include <Array.au3>
#include<EditConstants.au3>
#include<GUIConstants.au3>
#Include <GuiListBox.au3>
Opt("GUIOnEventMode", 1)
Global $Form1_1, $Form1_2 = 9999, $OK, $CompAddIp, $CompNameAdd, $Button1, $Button2, $Button3 = 9999
Global $datafile = "computers.txt"
Global $CompNameAddRead = GUICtrlRead($CompNameAdd)
Global $CompAddIpRead = GUICtrlRead($CompAddIp)
Dim $array, $i, $file, $CountLines, $List1, $text
Global $aArray[1000]
gui1()
Func gui1()
$Form1_1 = GUICreate("Process Terminator", 491, 301, 192, 124)
Global $List1 = GUICtrlCreateList("", 24, 64, 209, 188)
$Label1 = GUICtrlCreateLabel("Select the PC to terminate the process on:", 32, 40, 203, 17)
$Process = GUICtrlCreateInput("", 256, 64, 209, 21)
$Label2 = GUICtrlCreateLabel("Enter name of process to terminate:", 256, 40, 171, 17)
$Button1 = GUICtrlCreateButton("End Process", 256, 240, 211, 49)
GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
$Button2 = GUICtrlCreateButton("Add", 24, 264, 99, 25)
GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
$Button3 = GUICtrlCreateButton("Remove", 136, 264, 99, 25)
GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
$Label3 = GUICtrlCreateLabel("Process Terminator", 152, 8, 186, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function
     While 1
         Sleep(10)
     WEnd
EndFunc ;==>gui1
Func poplistbox()
$Countfile= _FileCountLines("computers.txt")
$file = "computers.txt"
For $i = 1 To $CountFile
$var1= FileReadLine($file, $i)
Global $text = $text & @CRLF & $var1
_GUICtrlListBox_AddString($List1, $text)
Next
EndFunc
Func gui2()
$Form1_2 = GUICreate("Add Computer", 227, 121, 212, 320)
GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function
$Label1 = GUICtrlCreateLabel("Computer Name", 8, 8, 80, 17)
$CompNameAdd = GUICtrlCreateInput("", 8, 32, 209, 21)
$OK = GUICtrlCreateButton("OK", 144, 88, 75, 25)
GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
$CompAddIP = GUICtrlCreateInput("", 8, 88, 121, 21)
$IP = GUICtrlCreateLabel("IP Address", 8, 64, 55, 17)
GUISetState(@SW_SHOW)
EndFunc ;==>gui2
Func On_Close()
     Switch @GUI_WINHANDLE ; See which GUI sent the CLOSE message
         Case $Form1_1
             Exit ; If it was this GUI - we exit <<<<<<<<<<<<<<<
         Case $Form1_2
             GUIDelete($Form1_2) ; If it was this GUI - we just delete the GUI <<<<<<<<<<<<<<<
             GUICtrlSetState($Button2, $GUI_ENABLE)
     EndSwitch
EndFunc

Func On_Button()
     Switch @GUI_CTRLID ; See which button sent the message
         Case $Button1
Case $Button2
             GUICtrlSetState($Button2, $GUI_DISABLE)
             gui2()
         Case $Button3
poplistbox()
Case $OK
poplistbox()
FileWriteLine($datafile, $CompNameAddRead & ',' & $CompAddIpRead)
GUIDelete($Form1_2) ; If it was this GUI - we just delete the GUI <<<<<<<<<<<<<<<
GUICtrlSetState($Button2, $GUI_ENABLE)
     EndSwitch
EndFunc
Edited by Cravin
Link to comment
Share on other sites

Okay, one last plea for help. I've simplified the script because the ability to add and remove computers from the list through the GUI isn't a requirement, so I've broken the script down to a single gui... Everything is working as it should except the killProc function. I want to pull the IP address from the selected listbox item and then execute the killProc function on that IP.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Include <Array.au3>
Opt("GUIOnEventMode", 1)
Global $Form1,$List1,$Input1,$Button1,$Countfile,$file
Dim $array, $i, $file, $CountLines, $List1, $text
Global $aArray[1000]
Main()
Func Main()
$Form1 = GUICreate("Form1", 429, 296, 858, 329)
$List1 = GUICtrlCreateList("", 16, 64, 201, 214)
$Label1 = GUICtrlCreateLabel("Select the PC to terminate the process on:", 16, 40, 203, 17)
$Label2 = GUICtrlCreateLabel("Process Terminator", 128, 8, 168, 28)
GUICtrlSetFont(-1, 16, 400, 0, "MS Sans Serif")
$Input1 = GUICtrlCreateInput("", 240, 64, 169, 21)
$Label3 = GUICtrlCreateLabel("Type name of process to terminate:", 240, 40, 170, 17)
$Button1 = GUICtrlCreateButton("Terminate Process", 240, 96, 169, 49)
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function
popListBox()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
EndFunc
Func popListBox()
$Countfile= _FileCountLines("computers.txt")
$file = "computers.txt"
For $i = 1 To $CountFile
$var1= FileReadLine($file, $i)
Global $text = $text & @CRLF & $var1
Global $CompName = StringSplit( $text, "," )
_GUICtrlListBox_AddString($List1, $CompName[1])
Next
EndFunc
Func On_Button()
Switch @GUI_CTRLID ; See which button sent the message
Case $Button1
killProc()
EndSwitch
EndFunc
Func On_Close()
Switch @GUI_WINHANDLE ; See which GUI sent the CLOSE message
     Case $Form1
             Exit ; If it was this GUI - we exit <<<<<<<<<<<<<<<
     EndSwitch
EndFunc

Func killProc()
;This script will kill a specified process.
Global $List1Text = ControlCommand("Process Terminator", "", "[CLASS:ListBox; INSTANCE:1]", "GetCurrentSelection", "")
Global $ipAddress = StringSplit( $list1Text, "," )
Dim $objWMIService, $objProcess, $colProcess
Dim $strComputer, $strProcessKill, $strInput
$strProcessKill = GUICtrlRead ($Input1)
$strProcessKill = "'" & $strProcessKill & "'"
; Input Box to get name of machine to run the process
Do
$strComputer = $ipAddress[2]
ConsoleWrite($strComputer)
If $strComputer <> "" Then
$strInput = 1
EndIf
Until $strInput = 1

$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colProcess = $objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & $strProcessKill )
For $objProcess in $colProcess
$objProcess.Terminate()
Next
EndFunc

Below is a sample computer.txt file that can be used for testing (should make an entry for your own pc that uses your current IP)

Computer01, 192.168.1.1
Computer02, 192.168.1.2
Computer03, 192.168.1.3
Computer04, 192.168.1.4
Computer05, 192.168.1.5
Computer06, 192.168.1.6
Computer07, 192.168.1.7
Computer08, 192.168.1.8
Computer09, 192.168.1.9
Computer10, 192.168.1.10
Computer11, 192.168.1.11
Computer12, 192.168.1.12
Computer13, 192.168.1.13
Computer14, 192.168.1.14
Computer15, 192.168.1.15
Computer16, 192.168.1.16
Computer17, 192.168.1.17
Computer18, 192.168.1.18
Computer19, 192.168.1.19
Computer20, 192.168.1.20
Computer21, 192.168.1.21
Computer22, 192.168.1.22
Computer23, 192.168.1.23
Computer24, 192.168.1.24
Computer25, 192.168.1.25
Computer26, 192.168.1.26
Computer27, 192.168.1.27
Computer28, 192.168.1.28
Computer29, 192.168.1.29
Computer30, 192.168.1.30
Link to comment
Share on other sites

It appears that the reason it's not working is because, for one thing, the script is running in OnEvent mode, but you never set an event for the button. Then you're using MessageLoop mode AND OnEvent mode to close the GUI, even though the MessageLoop GUIGetMsg will never return anything because the script is in OnEvent mode.

EDIT: Forgot to mention, your ControlCommand isn't necessary to get the text from the Listbox, you can use this instead, which actually returns the text from the selected item in the list box.

Global $List1Text = _GUICtrlListBox_GetText($List1, _GUICtrlListBox_GetCurSel($List1))

Also, there's no IP address in the listbox, so the string split and every thing after it is not going to work, in fact it causes an array subscript error as soon as it tries to read the return from it because there's only a computer name in the List.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...