简体   繁体   English

如何在远程计算机上执行命令并使用c#.net获取输出

[英]How to execute a command in a remote machine and get the output using c#.net

I am using WMI classes for executing command in remote machine and writing output in a file. 我正在使用WMI类在远程计算机中执行命令并在文件中写入输出。 I kept sleep for 5 sec if the process is completes with the 5 sec iam able to get the output from the file. 如果该过程完成,我保持睡眠状态5秒钟,而5秒钟可以从文件中获取输出。 But if the process is taking more time to finish iam not getting any out put. 但是,如果该过程需要更多的时间来完成,那么我就不会有任何投入。

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. 有人可以建议我如何等待远程过程完成,或者可以通过任何其他机制从远程计算机获取输出。

Following is my 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 + " > 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();

Below is one of the way you can wait on the remote process to complete it task 下面是您可以等待远程进程完成任务的一种方法

uint ProcessId = (uint)outParams["processId"];
    if (ProcessId != 0)
    {
        while (true)
        {
            try
            {
                Process.GetProcessById((int)ProcessId, {your_remoteMachineName});
                Thread.Sleep(1000);
            }
            catch
            {
                break;                    
            }
        }
    }

You could create a WCF service that creates the output to a given file, then returns an object instance that contains information eg number of pages in the file (the page size will depend on what you set your buffer size to in the WCF service). 您可以创建一个WCF服务,该服务创建到给定文件的输出,然后返回一个对象实例,该实例包含信息,例如文件中的页面数(页面大小取决于您在WCF服务中设置的缓冲区大小)。

eg create an operation contract Public Function execCommand(Byval commandName as String, Byval outputFile as String) as OutputInfo 例如,创建一个操作约定Public Function execCommand(Byval commandName作为String,Byval outputFile作为String)作为OutputInfo

Then create a second method that can retrieve the information page by page (possibly returning false when there are no more pages to retrieve or the page is out of range) 然后创建第二种方法,可以逐页检索信息(如果没有更多要检索的页面或该页面超出范围,则可能返回false)

Public Function getOutput(ByVal fileName as String, ByVal pageNo as Integer) as OutputPage 公共函数getOutput(ByVal fileName作为String,ByVal pageNo作为Integer)作为OutputPage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM