title: Impersonation with ManagementClass to launch Remote Process (VB)
body: <p>This code launches a program remotely. The problem I'm having is with access privileges, so I need to use impersonation in order to pass admin privileges with the program launch command/query. I don't know how to include impersonation with this, and I couldn't find anything useful on Google. I've tried things like connection options but I haven't been able to get anything to work. Any ideas?</p>
  
  <pre><code>    Dim sCmd As String = "C:\Program Files\Internet Explorer\IEXPLORE.EXE" ' &amp; txtData.Text.Trim
  
      ' add a reference to System.Management in Solution Explorer
  
      Dim wmi As ManagementClass
      Dim wmi_in, wmi_out As ManagementBaseObject
      Dim retValue As Integer
  
  
      Try
          wmi = New ManagementClass("\\" &amp; "ComputerName" &amp; "\root\cimv2:Win32_Process")
          ' get the parameters to the Create method
          wmi_in = wmi.GetMethodParameters("Create")
  
  
          ' fill in the command line plus any command-line arguments
          ' NOTE: the command can NOT be on a network resource!
          wmi_in("CommandLine") = sCmd
          ' do it!
          wmi_out = wmi.InvokeMethod("Create", wmi_in, Nothing)
          ' get the return code.  This not the return code of the
          ' application... it's a return code for the WMI method
          retValue = Convert.ToInt32(wmi_out("returnValue"))
          Select Case retValue
              Case 0
                  ' success!
              Case 2
                  Throw New ApplicationException("Access denied")
              Case 3
                  Throw New ApplicationException("Insufficient privilege")
              Case 8
                  Throw New ApplicationException("Unknown failure")
              Case 9
                  Throw New ApplicationException("Path not found")
              Case 21
                  Throw New ApplicationException("Invalid parameter")
              Case Else
                  Throw New ApplicationException("Unknown return code " &amp; retValue)
          End Select
      Catch ex As Exception
          MsgBox("sd" &amp; ": Can't create the process. " &amp; ex.Message)
      End Try
  </code></pre>