1

How to terminate a VBS File using Vb script.. I tried this code and it is not working,

Call StopProcessVBS(strComputer,strProcess)

Function StopProcessVBS (strComputerArg,strProcessArg)
    Set WshShell = CreateObject("WScript.Shell")
    Dim objWMIService, colProcessList
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
    For Each objItem in colItems
        If objItem.CommandLine = strProcessArg  Then
            **objItem.CommandLine.Terminate()**
        End If
    Next


    Set WshShell = Nothing
    Set objWMIService = Nothing
    Set colItems = Nothing
End Function
Ismail
  • 79
  • 2
  • 13
  • Have you tried searching for existing answers? Related question: http://stackoverflow.com/questions/893237/how-to-terminate-process-using-vbscript – Xenyal Nov 20 '14 at 16:27
  • In your mind, what's the difference between "VBS" and "Vb script"? – Jean-François Corbett Nov 20 '14 at 16:31
  • 1
    Define (in detail!) "not working"? Can you terminate a Commandline (as opposed to a process)? What do you pass as strProcessArgs? – Ekkehard.Horner Nov 20 '14 at 16:38
  • strProcess = "xxx.vbs" strComputer = "." Call StopProcessVBS(strComputer,strProcess) To terminate the process I should have used objItem.Terminate().... Please refer the corrected code below... – Ismail Nov 21 '14 at 03:35

1 Answers1

1

Finally Worked, Tried the below code

strComputer = "."
Call StopProcessVBS(strComputer,strProcess)

Function StopProcessVBS (strComputerArg,strProcessArg)
    Set WshShell = CreateObject("WScript.Shell")
    Dim objWMIService, colProcessList
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
    For Each objItem in colItems
        If Instr (1,Replace(objItem.CommandLine,"""",""),strProcessArg)  Then
            objItem.Terminate()
        End If
    Next

    Set WshShell = Nothing
    Set objWMIService = Nothing
    Set colItems = Nothing
End Function
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Ismail
  • 79
  • 2
  • 13