Various methodologies to Kill running Process in Task Manager. – User friendly Tech help
Approach 1:- Without using UFT and we will simply create a .VBS file on the system and Run it.
n
'*******************************************Sub***************************************************
'Function Name:- fn_KillMe
'Function Description:- Kills the Given process using Run Command
'Input Parameters:- sProcess,(Process Name)
'Output Parameters:- Process Killed Message
''**************************************************************************************************
msgbox fn_KillMe("Notepad.exe")
Public Function fn_KillMe(sProcess)
On Error Resume Next
Dim objShell,sStatus
'Creating WSH object
Set objShell = CreateObject("WScript.Shell")
'Executing the Run command to kill the Instance
sStatus = objShell.Run ("TASKKILL /F /IM "&sProcess)
'Check the Run command status (0 = Success)
if sStatus = 0 Then
fn_KillMe ="Running instances killed for the Application = '"&sProcess&"'"
Else
fn_KillMe ="Error occured while killing the Instance for the Application = '"&sProcess&"'"
End if
Set objShell =Nothing
If Err.Number 0 Then
fn_KillMe= "Not able to execute the process, Error occured = '"&Err.Description &"'"
End If
On Error Goto 0
End Function
n
Note:-
n
n
1. “TASKKILL /F /IM ”
nTaskkill is a command which ends one or more Processes
n/F – Forcefully closing the application
n/IM – Image Name. Nothing but the visible exe file in the task manager.
n
n
ImageName |
n
2.“TASKKILL /IM “
n
We can also use the command “TASKKILL /IM “, but incase any data is unsaved in the application instance to be killed,it will ask for user action.For example we have unsaved Notepad file, on killing of “Notepad.Exe” operation, we will get the save dialog box, and process won’t be killed until and unless we dont take any action on save dialog box.
n
Save Dialog Box |
n3.How to check version of WSH?
nwe can determine the Windows Script Host version number by typing CScript + press enter in command prompt.
n
n
Command Prompt |
n
A.2.
nUsing “Exec” Command
n
n
'*******************************************Sub***************************************************
'Function Name:- fn_KillMeExec
'Function Description:- Kills the Given process using Exec command
'Input Parameters:- sProcess,(Process Name)
'Output Parameters:- Process Killed Message
'**************************************************************************************************
msgbox fn_KillMeExec("Notepad.exe")
Public Function fn_KillMeExec(sProcess)
On Error Resume Next
Dim objShell,objExec,sStatus
'Creating WSH object
Set objShell = CreateObject("WScript.Shell")
'Executing the Run command to kill the Instance
Set objExec = objShell.Exec ("taskkill /IM "&sProcess)
'Check the Exec command status (1 = Success)
if objExec.Status = 1 Then
fn_KillMeExec ="Running instances killed for the Application = '"&sProcess&"'"
Else
fn_KillMeExec ="Error occured while killing the Instance for the Application = '"&sProcess&"'"
End if
Set objExec =Nothing
Set objShell =Nothing
If Err.Number 0 Then
fn_KillMeExec= "Not able to execute the process, Error occured = '"&Err.Description &"'"
End If
On Error Goto 0
End Function
n
nB.Using Windows Management Instrumentation (WMI), we will create “WMI Script” to fetch the running process and Terminate it.
n
n
'*******************************************Sub***************************************************
'Function Name:- fn_KillProcess
'Function Description:- Checks if a GIVEN process is running in Task Manager and Terminates it
'Input Parameters:- sProcess,(Process Name)
'Output Parameters:- Process Killed Message
'**************************************************************************************************
Msgbox fn_KillProcess("outlook.exe")
Public Function fn_KillProcess(sProcess)
Dim objWMI, objProcList
On Error Resume Next Err.Clear
Set objWMI = GetObject("winmgmts:\.rootcimv2")
'Look for the given process instance in Task Manager
Set objProcList = objWMI.ExecQuery("Select * from Win32_Process Where Name = '" & sProcess & "'")
'Incase Process is found inthe Task Manager
If objProcList.Count 0 Then
Dim iCnt :iCnt = 0
For Each objProc in objProcList objProc.Terminate
iCnt = iCnt + 1
Next
fn_KillProcess = iCnt&" Running instances killed for the Application = '"&sProcess&"'"
Else
fn_KillProcess = "No Running Instance found for the Application = '"&sProcess&"'"
End If
'Destroying the Objects
Set objWMI = Nothing
Set objProcList = Nothing
If Err.Number 0 Then
fn_KillProcess= "Not able to execute the process, Error occured = '"&Err.Description &"'"
End If
On Error Goto 0
End Function
n
n
nApproach 2:-UFT is required.
n
A.Creating object of Task Manager
n
n
'*******************************************Sub***************************************************
'Function Name:- fn_KillInstance
'Function Description:- Kills the Given process using Task Manager,UFT Required
'Input Parameters:- sProcess,(Process Name)
'Output Parameters:- N/A
''**************************************************************************************************
fn_KillInstance("EXCEL.EXE *32")
Public Function fn_KillInstance(sProcess)
'Using UFT objects
On Error Resume Next
'Launching the Task Manager Instance
SystemUtil.Run "taskmgr.exe"
'Incase Taskmanager is launched, select the Application instance to Kill
If Dialog("text:=Windows Task Manager").WinListView("nativeclass:=SysListView32").Exist(2) Then
'Incase We are not able to find the exact Image Name, Uncomment the below line and Run the code to get the Name
'print Dialog("text:=Windows Task Manager").WinListView("nativeclass:=SysListView32").GetROProperty ("all items")
'Selecting the instance of running application
Dialog("text:=Windows Task Manager").WinListView("nativeclass:=SysListView32").Select sProcess
'Clicking on End Process
Dialog("text:=Windows Task Manager").WinButton("text:=&End Process").Click
'Incase End Process Confirmation Window Exists
If Dialog("is owned window:=False","nativeclass:=#32770","text:=Windows Task Manager").Dialog("is owned window:=True","text:=Windows Task Manager").WinButton("text:=End process").Exist(2) Then
Dialog("is owned window:=False","nativeclass:=#32770","text:=Windows Task Manager").Dialog("is owned window:=True","text:=Windows Task Manager").WinButton("text:=End process").Click
End If
else
Print "Not able to launch the Task Manager instance so Exiting the Test"
Exit Function
End If
'Incase no instance is found for the Application in the Task Manager
If err.Number 0 Then
Print "Sorry no Instance found for the given Process = '"&sProcess&"' in the Task Manager"
Exit Function
End If
Print "Successfully Killed the Instance of Running Application in the Task Manager"
On Error Goto 0
End Function
n
B. Utilizing Send Keys Method and Descriptive Programming
n
n
'*******************************************Sub***************************************************
'Function Name:- fn_KillUsingSendKeys
'Function Description:- Kills the Given process using Task Manager using UFT
'Input Parameters:- sProcess,(Process Name)
'Output Parameters:- Status
''**************************************************************************************************
Msgbox fn_KillUsingSendKeys ("excel.exe")
Public Function fn_KillUsingSendKeys(sProcess)
'Using SendKeys Method
On Error Resume Next
Dim objShell,sStatus,objProcesses,iProcessId
'Creating WSH object
Set objShell = CreateObject("WScript.Shell")
'Executing the Run command to Launch Command Prompt
sStatus = objShell.Run ("cmd")
'Check the Run command status (0 = Success)
if sStatus = 0 Then
'Activates an application windows
Window("regexpwndtitle:=C:\Windows\System32\cmd.exe").Activate
'Using SendKeys Method to send Keystrokes to Cmd Prompt
objShell.SendKeys "cd.."
'Press Enter to Execute the command
objShell.SendKeys "{Enter}"
objShell.SendKeys "TASKKILL /F /IM "&sProcess
objShell.SendKeys "{Enter}"
fn_KillUsingSendKeys ="Running instances killed for the Application = '"&sProcess&"'"
Else
fn_KillUsingSendKeys ="Error occured while killing the Instance for the Application = '"&sProcess&"'"
End if
Set objShell =Nothing
End Function
n
Note :-
n
nWe can also use SytemUtil(Utlity Object) to kill the running processes
nExample:-To Kill process based on the Image Name(excel.exe) in task manager,
n
n Systemutil.CloseProcessByName “excel.exe”
n
n
Sytemtutil Methods |