Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for killprog.vbs

(view source code of killprog.vbs as plain text)

  1. ' KillProg.vbs,  Version 1.01
  2. ' Kill a running program.
  3. '
  4. ' Written by Rob van der Woude
  5. ' http://www.robvanderwoude.com
  6.  
  7.  
  8. ' Initialize variables
  9. strMsg   = ""
  10. numFound = 0
  11.  
  12. ' Check command line parameters
  13. Select Case WScript.Arguments.Count
  14. 	Case 0
  15. 		' At least a process should be specified
  16. 		Syntax
  17. 	Case 1
  18. 		' First parameter should be the process name
  19. 		' or "/?" to request online help
  20. 		strP2k = Wscript.Arguments(0)
  21. 		if InStr( strP2k, "?" ) > 0 Then Syntax
  22. 		strComputer = "."
  23. 	Case 2
  24. 		' First parameter should be the process name
  25. 		strP2k = Wscript.Arguments(0)
  26. 		if InStr( strP2k, "?" ) > 0 Then Syntax
  27. 		' Second parameter should be a computer name
  28. 		strComputer = Wscript.Arguments(1)
  29. 		if InStr( strComputer, "?" ) > 0 Then Syntax
  30. 	Case Else
  31. 		' Maximum is 2 command line parameter
  32. 		Syntax
  33. End Select
  34.  
  35. On Error Resume Next
  36. ' Query and running processes
  37. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  38. ' Display error number and description if applicable
  39. If Err.Number Then
  40. 	strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf & _
  41. 	         Err.Description & vbCrLf & vbCrLf
  42. 	Syntax
  43. End If
  44. On Error GoTo 0
  45. ' Kill specified process
  46. Set colItems = objWMIService.ExecQuery( "Select * from Win32_Process", , 48 )
  47. For Each objItem in colItems
  48. 	If LCase( objItem.Caption ) = LCase( strP2k ) Then
  49. 		numFound = numFound + 1
  50. 		Wscript.Echo "Terminating " & objItem.Caption
  51. 		objItem.Terminate
  52. 	End If
  53. Next
  54.  
  55. ' Check if any process was found
  56. If numFound = 0 Then
  57. 	WScript.Echo "No process named " & Chr(34) & strP2k & Chr(34) & " was found"
  58. End If
  59.  
  60. ' Done
  61. WScript.Quit(0)
  62.  
  63.  
  64. Sub Syntax
  65. 	strMsg = strMsg & vbCrLf & "KillProg.vbs,  Version 1.01" & vbCrLf & _
  66. 	         "Terminate (" & Chr(34) & "kill" & Chr(34) & _
  67. 	         ") a running program." & vbCrLf & vbCrLf & _
  68. 	         "Usage:  CSCRIPT  KILLPROG.VBS  prog_name  [ computer_name ]" & _
  69. 	         vbCrLf & vbCrLf & _
  70. 	         "Where:  " & Chr(34) & "prog_name" & Chr(34) & _
  71. 	         "     is the name of the program to be terminated" & vbCrLf & _
  72. 	         "        " & Chr(34) & "computer_name" & Chr(34) & _
  73. 	         " is the name of the computer where " & Chr(34) & _
  74. 	         "prog_name" & Chr(34) & " is" & vbCrLf & _
  75. 	         "                        to be terminated" & _
  76. 	         vbCrLf & vbCrLf & _
  77. 	         "Written by Rob van der Woude" & vbCrLf & _
  78. 	         "http://www.robvanderwoude.com" & vbCrLf
  79. 	WScript.Echo strMsg
  80. 	' Abort with return code 1
  81. 	WScript.Quit(1)
  82. End Sub
  83.  

page last modified: 2024-04-16; loaded in 0.0101 seconds