Solved

WMI to see result of remote command

Posted on 2008-06-10
8
2,693 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
Comment Utility
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
Comment Utility
When I try that I get an unrecognized or incomplete command error...
0
 
LVL 41

Expert Comment

by:graye
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

Author Comment

by:invisiblekhaos2
Comment Utility

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 125 total points
Comment Utility
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
Comment Utility
Thanks, I never saw your message though... found the answer on another website...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now