How to execute a command in a remote machine and get the output using c#.net
<p>Hi 
I am using WMI classes for executing command in remote machine and writing output in a file.
I kept sleep for 5 sec if the process is completes with the 5 sec iam able to get the output from the file.
But if the process is taking more time to finish iam not getting any out put.</p>

<p>Can any one suggest me how to wait till the remote process is completes or any other mechanism to get the output from from remote machine.</p>

<p>Following is my code</p>

<pre><code> ConnectionOptions connOptions = new ConnectionOptions();
            ObjectGetOptions objectGetOptions = new ObjectGetOptions();
            ManagementPath managementPath = new ManagementPath("Win32_Process");
            connOptions.Impersonation = ImpersonationLevel.Impersonate;
            connOptions.EnablePrivileges = true;
    ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", computerName), connOptions);
                        manScope.Connect();
                        // Method Options
                        InvokeMethodOptions methodOptions = new InvokeMethodOptions(null, System.TimeSpan.MaxValue);
                        ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
                        ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
                        // Console.WriteLine(command);
                        inParams["CommandLine"] = "cmd /c " + command + " &gt; c:\\tmp_dr.dat";
                        ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams,methodOptions);

                        Thread.Sleep(5000);

                        // then reading the file.

                        StreamReader sr = new StreamReader("\\\\" + computerName + "\\c$\\tmp_dr.dat");
                        line = sr.ReadLine();
                        while (line != null)
                        {enter code here
                            result += line + "\n";
                            line = sr.ReadLine();
                        }



                        sr.Close();
</code></pre>
