Solved

WMI to see result of remote command

Posted on 2008-06-10
8
2,705 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

22 Experts available now in Live!

Get 1:1 Help Now