locked
VB Script to Kill the process ID RRS feed

  • Question

  • Hi All,

    My requirement is to create a VB script to kill the process which is running under svchost.exe.

    I have written the below script and it is throwing an error

    " Object Doesn't support this property or method" 'ObjProcess.terminate'

    The script works fine till printing the process id when I try to terminate I throws the below error.

    Any assistance is highly appreciated.

    ===============================================

    set objIdDictionary = CreateObject("Scripting.Dictionary")
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colServices = objWMIService.ExecQuery _
     ("Select * from Win32_Service Where name ='WDSServer'")
    For Each objService in colServices
     If objIdDictionary.Exists(objService.ProcessID) Then
     Else
     objIdDictionary.Add objService.ProcessID, objService.ProcessID
     End If
    Next
    colProcessIDs = objIdDictionary.Items
    For i = 0 to objIdDictionary.Count - 1
     Set colServices = objWMIService.ExecQuery _
     ("SELECT * FROM Win32_Service WHERE ProcessID = '" & _
     colProcessIDs(i) & "'")

    'Wscript.Echo "Process ID: " & colProcessIDs(i)

    For Each objProcess in colServices
      objProcess.Terminate()

     Next
    Next

    ===============================================

    Regards,
    Satish Phatge.


    Tuesday, June 16, 2015 9:48 AM

Answers

  • Here is a method you can play with:

    Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate, (Debug)}\\W8test\root\cimv2")
    Set svc = wmi.Get("Win32_Service.Name='alg'")
    Set p = wmi.Get("Win32_Process.Handle=" & svc.ProcessID )
    msgbox p.terminate()


    \_(ツ)_/


    • Edited by jrv Tuesday, June 16, 2015 12:34 PM
    • Marked as answer by SatishPhatge Tuesday, June 16, 2015 12:53 PM
    Tuesday, June 16, 2015 12:33 PM
  • Thanks Jrv it is working for me now.
    • Marked as answer by SatishPhatge Tuesday, June 16, 2015 12:53 PM
    Tuesday, June 16, 2015 12:53 PM

All replies

  • Too much unnecessary code:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colServices = objWMIService.ExecQuery("Select * from Win32_Service Where name ='WDSServer'") For Each objService in colServices objService.StopService Next


    \_(ツ)_/



    • Edited by jrv Tuesday, June 16, 2015 10:37 AM
    Tuesday, June 16, 2015 10:29 AM
  • In PowerShell:

    (Get-WmiObject win32_service -filter 'Name="WDSService"').StopService()

    OR

    Get-Service WDSService | Stop-Service -force


    \_(ツ)_/

    Tuesday, June 16, 2015 10:40 AM
  • How t terminate the process a service is running under:

    Get-WmiObject Win32_Service -filter 'Name="WDSService"'|%{(Get-Process -id $_.ProcessID).Terminate()}


    \_(ツ)_/

    Tuesday, June 16, 2015 10:58 AM
  • Thanks for the response.

    Stop service doesn’t work as it goes in hung state.

    We have to kill the process to stop the service. Since service run under svchost.exe I cannot kill the process directly.

    The only option is to find the process id and kill it.

    The last reply looks good to me. Let me try.

    Thanks again.



    Tuesday, June 16, 2015 11:14 AM
  • The last reply is a Powershell script and I am looking for a VB script.

    can I have similler in VB script ?

    Tuesday, June 16, 2015 11:19 AM
  • Use PowerShell it is easier.  You can convert it to VBScript very easily.  I really don't feel like writing custom scripts.  If you don't want to write it then you can find a consultant that will likely bemore than happy to do it for you.

    Just follow the template.  Get the service by name and us the ProcessId of the service to kill the process.


    \_(ツ)_/

    Tuesday, June 16, 2015 11:36 AM
  • Here is a method you can play with:

    Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate, (Debug)}\\W8test\root\cimv2")
    Set svc = wmi.Get("Win32_Service.Name='alg'")
    Set p = wmi.Get("Win32_Process.Handle=" & svc.ProcessID )
    msgbox p.terminate()


    \_(ツ)_/


    • Edited by jrv Tuesday, June 16, 2015 12:34 PM
    • Marked as answer by SatishPhatge Tuesday, June 16, 2015 12:53 PM
    Tuesday, June 16, 2015 12:33 PM
  • Thanks Jrv it is working for me now.
    • Marked as answer by SatishPhatge Tuesday, June 16, 2015 12:53 PM
    Tuesday, June 16, 2015 12:53 PM