This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

vbscript to kill a process on a remote windows server

I am using apmservicecontrol to stop and restart services on monitored windows servers without issue.  Now I have a need to completely stop remote processes (not services) on that same server.  I cannot find a built in way to do this. 

Is there a built in way to do this and if not, does anyone have a simple vbscript that I can paste into the script body part of the Windows Script Monitor, that can maybe kill the process?

Thanks

  • Hi Rick,

    this process killing VB script for Windows Script Monitor should do the work. Use ${IP} [PROC_NAME] as script arguments. It outputs number of killed processes in statistics.

    ' APM Kill Process
    ' Arguments: ${IP} [PROC_NAME]

    Option Explicit
    On Error Resume Next

    Const PROCESS_STOPPED = 1, EMPTY_RESULTSET = 0, INVALID_PARAMS = -1, OBJECT_NOT_INITIALIZED = -2

      Dim lstArgs, computerName, procName, objSWbemServices, resultSetSWbemObject, element, objProcess, processCount

      Set lstArgs = WScript.Arguments

      If lstArgs.Count = 2 Then
         computerName = Trim( lstArgs( 0 ))
         procName     = Trim( lstArgs( 1 ))
      Else
         WScript.Echo( "Kill process arguments: ${IP} [PROC_NAME]" )
         WScript.Quit( INVALID_PARAMS )
      End If
     
      Set objSWbemServices = GetObject( "winmgmts:\\" & computerName & "\root\cimv2" )

      If( IsEmpty( objSWbemServices ) = True ) Then
        WScript.Echo( "OBJECT_NOT_INITIALIZED :: " & computerName )
        WScript.Quit( OBJECT_NOT_INITIALIZED )
      End If

      Set resultSetSWbemObject = objSWbemServices.ExecQuery( "SELECT Name FROM Win32_Process WHERE Name = """ & procName & """" )

      processCount = 0
      For Each objProcess in resultSetSWbemObject
        objProcess.Terminate()
        processCount = processCount + 1
      Next

      If( IsEmpty( resultSetSWbemObject ) = True ) Then
        WScript.Echo( "OBJECT_NOT_INITIALIZED :: WMI ResultSet " )
        WScript.Quit( OBJECT_NOT_INITIALIZED )
      End If

      WScript.Echo( "Statistic:" & processCount )
      If( resultSetSWbemObject.Count = 0 ) Then
        WScript.Echo( "EMPTY_RESULTSET" )
        WScript.Quit( EMPTY_RESULTSET )
      Else
        WScript.Echo( "PROCESS_STOPPED :: " & LCase( procName ))
        WScript.Quit( PROCESS_STOPPED )
      End If