Question : Need Help Converting VB Script/WMI code into VB.NET Code (Remote Process Kill)

As the subject implies, I need help converting VB Script / WMI code into VB.NET.

I need this utility to kill processes on remote machines:


Here is the current VB Script/WMI code that works:
*****************************************************
Option Explicit
Dim objShell
Dim objNet
Dim strTitle
Dim mach
Dim Computer
Dim CompHost
Dim Process
Dim Processes
Dim StpScript
Dim StpService
Dim objWMIService
Dim colServices
Dim objService
Dim errReturnCode
Dim colProcessList
Dim objProcess

Set objShell = CreateObject("wscript.shell")
Set objNet = CreateObject("wscript.network")

mach = "test_machine_name"

Processes = Array("'radexecd'","'radsched'","'radstgms'","'radpinit'","'radskman'","'radrexxw'","'radiamsi'","'radconct'","'radpnlwr'","'nvdutils'","'wscript'")            

 
  For Each Process In Processes
 Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & mach & "\root\cimv2")
 Set colServices = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Service WHERE Name = "&Process&"")
  For Each objService in colServices
    errReturnCode = objService.StopService()
  Next
    Next



*******************************************************************
Now, Here is what I have in my Visual Studio VB.net form
*******************************************************************

Dim objShell
        Dim objNet
        Dim strTitle
        Dim mach
        Dim Computer
        Dim CompHost
        Dim Process
        Dim Processes As New ArrayList
        Dim StpScript
        Dim StpService
        Dim objWMIService
        Dim colServices
        Dim objService
        Dim errReturnCode
        Dim colProcessList
        Dim objProcess

        'On Error Resume Next

        mach = ComboBox2.Text

        Processes.Add("radskman")
        Processes.Add("radpinit")
        Processes.Add("radconct")
        Processes.Add("radrexxw")

        Dim strWinMgt
        Dim strQuery, strComputerName
        Dim Process_Collection


        For Each Process In Processes
            strWinMgt = "winmgmts://" & strComputerName & ""
            strQuery = "select * from Win32_Process where Name = " & Process & ""
            Process_Collection = GetObject(strWinMgt).ExecQuery(strQuery)

            For Each objService In Process_Collection
                errReturnCode = objService.StopService()
            Next
        Next



**************

The only real difference I am seeing here is around this line in the WMI script:

 Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & mach & "\root\cimv2")

**************


Im not sure how to set the impersonationLevel in VB.net like it should be.....

Results are if I use the .NET form, it does not stop the processes, but the VB script does.... can anyone help?

Answer : Need Help Converting VB Script/WMI code into VB.NET Code (Remote Process Kill)

Hey phesser,

The modified code below is tested and successfully kills the process named myapp.exe.  I've only tested on current computer, but WMI shouldn't have any problems killing the processes on remote machines if you have the appropriate access.  This code requires a reference to System.Management.

        Dim strProcess As String
        Dim methodArgs() As Object = {}
        Dim strComputer As String = "."
        Dim options As New ConnectionOptions

        strProcess = "myapp.exe"

        options.Impersonation = ImpersonationLevel.Impersonate
        options.EnablePrivileges = True

        Dim ms As New ManagementScope("\\" & strComputer & "\root\CIMV2", options)
        Dim q As New SelectQuery("select * from Win32_Process where Name='" & strProcess & "'")
        Dim search As New ManagementObjectSearcher(ms, q)

        Dim wmiprocess As ManagementObject
        For Each wmiprocess In search.Get()
            wmiprocess.InvokeMethod("Terminate", methodArgs)
        Next
Random Solutions  
 
programming4us programming4us