Remote Execute Process

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
I'm trying to make a class where I can remotely execute processes.

The code I have below gives me a Return Value of Successful Completion and when looking at the processes in Task Manager I do see a process with the PID returned.

The problem is that the process is created and does nothing. :(

If somebody could point me in the right direction it would be greatly appreciated.

VB.NET:
Imports System.Management

Public Class Form1

    Dim ReturnCodes As New Dictionary(Of Integer, String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        InitDictionary()

        Dim ReturnValue As String = _
            RunCommand("CMD.EXE /c " & ControlChars.Quote & "C:\<path>\My.bat" & ControlChars.Quote, _
                "MachineName", _
                "Domain\User", _
                "Password")

        MessageBox.Show(ReturnValue)

    End Sub

    Private Sub InitDictionary()
        With ReturnCodes
            .Add(0, "Successful Completion")
            .Add(2, "Access Denied")
            .Add(3, "Insufficient Priviledge")
            .Add(8, "Unknown Failure")
            .Add(9, "Path Not Found")
            .Add(21, "Invalid Parameter")
        End With
    End Sub

    Public Function RunCommand(ByVal Command As String, _
                                ByVal MachineName As String, _
                                ByVal UserName As String, _
                                ByVal Password As String) As String

        Dim ConnectOptions As New System.Management.ConnectionOptions
        ConnectOptions.Username = UserName
        ConnectOptions.Password = Password

        Dim Path As New System.Management.ManagementPath("\\" & MachineName & "\root\cimv2:Win32_Process")
        Dim Scope As New System.Management.ManagementScope(Path, ConnectOptions)

        Scope.Connect()

        Dim ObjectOptions As New System.Management.ObjectGetOptions()
        Dim Instance As New System.Management.ManagementClass(Scope, Path, ObjectOptions)

        Dim InputParameters As System.Management.ManagementBaseObject = Instance.GetMethodParameters("Create")
        InputParameters("CommandLine") = Command

        Dim OutputParameters As System.Management.ManagementBaseObject = _
            Instance.InvokeMethod("Create", InputParameters, Nothing)

        Return "Status: " & ReturnCodes(OutputParameters("ReturnValue")) & Environment.NewLine & _
            "Process ID: " & OutputParameters("ProcessId")
    End Function

End Class
 
Back
Top