Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

WMI to see result of remote command

Posted on 2008-06-10
8
Medium Priority
?
2,930 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:invisiblekhaos2
  • 4
  • 3
8 Comments
 
LVL 41

Expert Comment

by:graye
ID: 21752865
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.
0
 

Author Comment

by:invisiblekhaos2
ID: 21753056
When I try that I get an unrecognized or incomplete command error...
0
 
LVL 41

Expert Comment

by:graye
ID: 21755666
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
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:invisiblekhaos2
ID: 21803002
None of these answers worked... I found out a solution that works for me...
0
 

Author Comment

by:invisiblekhaos2
ID: 21816113

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

0
 
LVL 41

Accepted Solution

by:
graye earned 250 total points
ID: 21821113
Isn't that solution exactly what I had suggested?    Namely, to redirect the output to a file, wait a while, and then read the contents of the file
0
 

Author Closing Comment

by:invisiblekhaos2
ID: 31465704
Thanks, I never saw your message though... found the answer on another website...
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

971 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