Solved

WMI to see result of remote command

Posted on 2008-06-10
8
2,766 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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 125 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 63
Why use this lambda? 12 95
How to repeat the data 4 42
How we can add Dten600.dll in GAC in c#. 18 53
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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