WMI – start a process on remote machine and passing custom credentials.

I use System.Management in .NET 2.0 to connect, pass custom credentials to a remote machine and query with WMI frequently.   This is straight forward and there are many examples showing the syntax how to do this.   However, while working with remote IIS7 installs,  I wanted to execute a command on a remote machine while passing custom credentials.  I couldn’t find an example that provided this functionality, hopefully this will save someone time searching for the syntax.  This sample starts an instance of ‘calc.exe’ on a remote machine. 


The code accepts four parameters



  • Command to run,
  • Machine to connect to,
  • UserName,
  • Password





Module Module1


    Sub Main()
        Dim retValue As String
        retValue = RunCommand(“calc.exe”, “MachineName”, “MachineNameUserID”, “Password”)
        Console.WriteLine(retValue)
    End Sub


    Function RunCommand(ByVal strCommand As String, ByVal strMachineName As String, ByVal strUserName As String,
ByVal strPassword As String) As String
        Dim options As New System.Management.ConnectionOptions
        options.Username = strUserName
        options.Password = strPassword


        Dim path As New System.Management.ManagementPath(“\” & strMachineName & “rootcimv2:Win32_Process”)
        Dim scope As New System.Management.ManagementScope(path, options)


        scope.Connect()


        Dim opt As New System.Management.ObjectGetOptions()
        Dim classInstance As New System.Management.ManagementClass(scope, path, opt)


        Dim inParams As System.Management.ManagementBaseObject = classInstance.GetMethodParameters(“Create”)
        inParams(“CommandLine”) = strCommand


        ‘ Execute the method and obtain the return values.
        Dim outParams As System.Management.ManagementBaseObject = classInstance.InvokeMethod(“Create”, inParams, Nothing)
        Return “ReturnValue:” & outParams(“returnValue”) & ” Process ID: {0}” & outParams(“processId”)


    End Function


End Module


Some links I found useful



Return code s
Description (these are the codes returned once a process has executed)

0 Successful completion
2 Access denied
3 Insufficient privilege
8 Unknown failure
9 Path not found
21 Invalid parameter

5 thoughts on “WMI – start a process on remote machine and passing custom credentials.”

  1. I exactly used your code it runs fine but nothing happens on the remote machine it occured on one machine but on the other machine it gives me the error like access denied.

    Like

  2. It starts the application but its not visible on the remote system. do you know how to make it visible or to make part of a opened session?

    Like

  3. Processes launched by WMI can NEVER be visible. They run in their own windowstation, the same as window services. It make it visible you have to have a System Tray application or similar hidden application which is launched when each desktop starts. Then you have to setup some kind of IPC/RPC between the system tray app and your server.

    Like

  4. The processer used for this execution is the remote processer(where exe is located) or the local processer(where Package is running)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s