Sign in to follow this  
Followers 0
DrewDennis

Newbie Needing Help

11 posts in this topic

Hi! I am new to autoit and need a bit of help. I found the autoit script below, and need to make some changes , but need experts input. I need to pass alternate domain credentials so users can kill PIDS on remote servers, but not have admin access. I can create a service account and would like the userid/password hard coded in the script when I compile it to an exe. And second.. I'd like it to not kill the pid when the user hits the kill button, I'd like it to pop up another window for confirmation before it sends the command. Thanks in Advance!!

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=..\..\icons\procKill.ico
#AutoIt3Wrapper_outfile=RemoteProcKill.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include
#include
#include


$strComputer = ""
global $lvProcessList ;Create the global varible which is used to create the list of open applications
$wbemFlagReturnImmediately = 0x10 ; parameter needed for wmi
$wbemFlagForwardOnly = 0x20 ; parameter needed for wmi
$oMyError = ObjEvent("AutoIt.Error", "wmiError") ; Install a custom error handler

;Define the GIU
GUICreate("Remote Proccess Killer", 310, 400, 100, 200)
GUICtrlCreateLabel("Enter the computer name then select the process to kill:", 10, 15)
$inpComputerName = GUICtrlCreateInput("", 10, 40, 200)


;create a refresh button that when pressed calls the GetWinList() function
$btnGetProcessList = GUICtrlCreateButton("Get Processes", 215, 40, 85,"", $BS_DEFPUSHBUTTON )

;Create the List view to hold all process name/pids
CreateProcessListView()


;Define the buttons
$btnKill = GUICtrlCreateButton("Kill Process", 20, 360, 125, 20)
$btnExit = GUICtrlCreateButton("Exit", 170, 360, 125, 20)

;Start the GUI
GUISetState()
Do
;wait for a user action
$n = GUIGetMsg()
If $n = $btnKill Then

;List view returns results with at | seperating items, here we are spliting that
$arrProcessSplit = StringSplit(guictrlread(GUICtrlRead($lvProcessList)), "|")

if $arrProcessSplit[0] > 0 Then
;removing all trailing white space and calling function to kill the process
$procName = StringStripWS($arrProcessSplit[1], 2)
$pid = StringStripWS($arrProcessSplit[2], 2)
call("KillProcess", $procName, $pid) ; call function KillProcess pass process name pid to it
endif

elseIf $n = $btnExit Then

;leave applicaiton if the exit button is pressed
Exit

elseif $n = $btnGetProcessList then

;when the Get Process button is pressed then read the name computer name that the user
;entered
$strComputer = guictrlread($inpComputerName)

if $strComputer <> "" Then ; check to make sure it isnt null

if ping($strComputer, 1000) >0 then ;check to make sure that the computer is alive

CreateProcessListView() ;call this function again to elimanate duplicate process if the Get Process button is click more then once

;sets the computer name and call objget func to complete allow for wmi query
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
call("GetProcessList") ;call the function to display a list of all the running processes

Else

MsgBox(48, "Error", "Host unreachable") ; error message if host isnt reachable

EndIf

EndIf ; endif for null computer name

EndIf

Until $n = $GUI_EVENT_CLOSE


;error message to display if calling wmi functions end in an error,
;mostly to catch unathorized users trying to access computers they dont have permissions to
Func wmiError()
;MsgBox(64, "Error", "There was an issue connecting to the remote computer. Please verify that you have permissions to this computer")
EndFunc

;delete any gui control
;then creates a list view gui control
Func CreateProcessListView()
GUICtrlDelete($lvProcessList)
$lvProcessList = GUICtrlCreateListView("Process Name |PID", 10, 75, 290, 265, BitOR($LVS_SORTASCENDING, $LVS_SINGLESEL))
EndFunc


Func KillProcess($processName, $pid)

;display a message box to confirm kill process
$confirmKill = MsgBox(36,"Kill Process", "Are you sure you want to kill the process " & $processName & " on computer " &$strComputer & "?")
if $confirmKill = 6 Then ;if user answers yes to the above question

;user wmi to select the find the process named what the user choose and
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process where Handle = '" & $pid & "'", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) Then
For $objItem In $colItems
$objItem.Terminate ;terminal process
Next
EndIf

;refreshes the listview
CreateProcessListView()
GetProcessList()

EndIf ; endif for msgbox question, yes
EndFunc

func GetProcessList()

;select all running processes via wmi
$colItems = $objWMIService.ExecQuery("SELECT Name FROM Win32_Process", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

;display all processes in the list view
If IsObj($colItems) Then
For $objItem In $colItems
GUICtrlCreateListViewItem($objItem.Name & "|" & $objItem.Handle, $lvProcessList)
Next
EndIf

EndFunc

Share this post


Link to post
Share on other sites



Welcome to AutoIt and the forum!

Ad 1) I wouldn't store userid/password for such a powerful user in a script. A compiled AutoIt script is no save place! If you do this in a company environment I'm sure your CIO will kill you.

Ad 2) Function KillProcess already has a confirm message. What else do you need?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hi, do you have a question?

Also, where did you "find" this script?

Script was found via wonderful google search! Its what brought me to Autoit!

And the post above, there are only 4 users that will have this compiled script, so access to the service account is not a concern, we just don't want them to have Administrator access to the boxes. What I was wanting in the confirm was it to list the PID ID its going to kill.... Thansk!

Share this post


Link to post
Share on other sites

Welcome to AutoIt and the forum!

Ad 1) I wouldn't store userid/password for such a powerful user in a script. A compiled AutoIt script is no save place! If you do this in a company environment I'm sure your CIO will kill you.

Ad 2) Function KillProcess already has a confirm message. What else do you need?

And the Boss does know :-) we're a pretty small environment, so all is well with it...for now. The CORRECT solution is to fix our app so we don't HAVE to kill PIDS...but thats months down the road.

Share this post


Link to post
Share on other sites

To show the PID alter this line in function KillProcess:

$confirmKill = MsgBox(36,"Kill Process", "Are you sure you want to kill the process " & $processName & " on computer " &$strComputer & "?")
to
$confirmKill = MsgBox(36,"Kill Process", "Are you sure you want to kill the process " & $processName & " (PID: " & $PID & ") on computer " & $strComputer & "?")


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

To show the PID alter this line in function KillProcess:

$confirmKill = MsgBox(36,"Kill Process", "Are you sure you want to kill the process " & $processName & " on computer " &$strComputer & "?")
to
$confirmKill = MsgBox(36,"Kill Process", "Are you sure you want to kill the process " & $processName & " (PID: " & $PID & ") on computer " & $strComputer & "?")

Thanks! Thats PERFECT! Any thoughts on passing credentials inside the script?

Share this post


Link to post
Share on other sites

That's a bit more complex because you have to modify the WMI call. I remember there was a thread just a few weeks ago.

Will post as soon as I find anything.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
shows you how to modify the WMI query and pass userid/password.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

shows you how to modify the WMI query and pass userid/password.

Tried adding code change as below, and received error: Incorrect number of parameters in function call. And...since I am a newb...I am out of posts for the day. :thumbsdown:

;sets the computer name and call objget func to complete allow for wmi query
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2", $strUser, $strPassword)
call("GetProcessList") ;call the function to display a list of all the running processes

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Why do you use call? Just call the function directly. And you need to change the way you define $objWMIService if you need to pass credentials:

$objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objSWbemLocator.ConnectServer($machine, "root\cimv2", $strUser, $strPassword)
GetProcessList() ;call the function to display a list of all the running processes
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


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
Sign in to follow this  
Followers 0