Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-09-16
6
1,863 Views
Last Modified: 2012-08-14
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?
0
Comment
Question by:phesser
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:sj_hicks
ID: 12081441
Note that you should be using the Terminate.  The StopService method does not exist in Win32_Process.

Below is code I've use to call the WMI shutdown WMI method in VB.NET (*tested*).  This shows how one way of executing WMI methods ion VB.NET:

        Dim strComputer As String = "."
        Dim options As New ConnectionOptions
        options.Impersonation = ImpersonationLevel.Impersonate
        options.EnablePrivileges = True
        ' Select all from WMI class Win32_OperatingSystem
        Dim ms As New ManagementScope("\\" & strComputer & "\root\CIMV2", options)
        Dim q As New SelectQuery("SELECT * FROM Win32_OperatingSystem")
        Dim search As New ManagementObjectSearcher(ms, q)

        ' methodArgs defines the options to shutdown with
        Dim methodArgs() As Object = {shutdownFlags, 0}
        ' enum each entry for Win32_OperatingSystem and call WMI Win32Shutdown method
        Dim os As ManagementObject
        For Each os In search.Get()
            os.InvokeMethod("Win32Shutdown", methodArgs)
        Next


So you'd probably need to do something like the following (*untested*) to terminate processes:

        Dim strComputer As String = "."
        Dim options As New ConnectionOptions
        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=" & strProcessName)
        Dim search As New ManagementObjectSearcher(ms, q)

        Dim os As ManagementObject
        For Each os In search.Get()
            os.InvokeMethod("Terminate")
        Next


Hope this helps.
0
 

Author Comment

by:phesser
ID: 12084206
sj_hicks,

I tried your process and had 2 problems, here is my code snippet::

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

        Dim strWinMgt
        Dim strQuery, strComputerName
        Dim Process_Collection

        Dim strComputer As String = "."
        Dim options As New ConnectionOptions
        options.Impersonation = ImpersonationLevel.Impersonate
        options.EnablePrivileges = True

        For Each Process In Processes
            Dim ms As New ManagementScope("\\" & strComputer & "\root\CIMV2", options)
            Dim q As New SelectQuery("select * from Win32_Process where Name=" & Process & "")
            Dim search As New ManagementObjectSearcher(ms, q)

            Dim os As ManagementObject
            For Each os In search.Get()
                os.InvokeMethod("Terminate")
            Next
        Next


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

I had to format the line below differently:
Dim q As New SelectQuery("select * from Win32_Process where Name=" & Process & "")

And this line was getting an error " Overload resolution failed becase no "InvokeMethod" accepts this number of arguments"
os.InvokeMethod("Terminate")

Have any clues on how to fix it?
0
 

Author Comment

by:phesser
ID: 12087093
Raising the points, really need an answer on this one...

IMPORTANT NOTE:  These are processes that are 'spawned' from services..these are not services directly, if that makes a difference.

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:phesser
ID: 12089599
Also note, applications like PSKill work to kill these processes...thats what I am trying to mimic but with inherent vb.net code.

Increasing points to 350.
0
 
LVL 7

Accepted Solution

by:
sj_hicks earned 400 total points
ID: 12098131
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
0
 

Author Comment

by:phesser
ID: 12103358
sj_hicks, you are the man, appreciate the help...that works quite well :)

Increasing the points to 400 for exceptional work.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question