Link to home
Start Free TrialLog in
Avatar of invisiblekhaos2
invisiblekhaos2

asked on

WMI to see result of remote command

I am trying to use WMI to execute a command on a remote computer (which I found out how to do), but what I need now is to see the actual result of the command. For example, if I run the ipconfig command I would like to see the result come back to my remote station. Is there any way to do this?
Avatar of graye
graye
Flag of United States of America image

No, not really....

You *could* redirect the output of the command into a local text file, wait a few miliseconds for the file to close, and then read the contents of the file.
Avatar of invisiblekhaos2
invisiblekhaos2

ASKER

When I try that I get an unrecognized or incomplete command error...
Since the redirection actually uses the Command Shell, you'd have to modify your command to look like this:

        wmi = New ManagementClass("\\" & PC & "\root\cimv2:Win32_Process")
        wmi_in = wmi.GetMethodParameters("Create")
        wmi_in("CommandLine") = "cmd /c C:\SomeCommand.exe > C:\Temp\Output.txt"
        wmi_out = wmi.InvokeMethod("Create", wmi_in, Nothing)
        ret = Convert.ToInt32(wmi_out("returnValue"))
        If ret <> 0 Then
            Throw New ApplicationException("Create method error " & ret)
        End If
None of these answers worked... I found out a solution that works for me...

public String ExecuteRemoteCommands(String computerName, String command)
        {
            String line = null;
            String result = null;
            ConnectionOptions connOptions = new ConnectionOptions();
            ObjectGetOptions objectGetOptions = new ObjectGetOptions();
            ManagementPath managementPath = new ManagementPath("Win32_Process");
            connOptions.Impersonation = ImpersonationLevel.Impersonate;
            connOptions.EnablePrivileges = true;
            try
            {
                ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", computerName), connOptions);
                manScope.Connect();
                ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
                ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
                inParams["CommandLine"] = "cmd /c " + command + " > c:\\tmp.txt";
                ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
                Thread.Sleep(5000);
                StreamReader sr = new StreamReader("\\\\" + computerName + "\\c$\\tmp.txt");
                line = sr.ReadLine();
                while (line != null)
                {
                    result += line;
                    line = sr.ReadLine();
                }
                sr.Close();
            }
            catch (Exception exception)
            {
                exception.Message.ToString();
            }
            return result;
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, I never saw your message though... found the answer on another website...