c#遠程執行命令備忘

來源:互聯網
上載者:User

       c#中可以通過wmi在遠程機上執行命令(wmi:windows management interface 可以通過一個公用的介面訪問不同作業系統(windows系統)的構成單元,利用它可以高效的管理遠程和本地的電腦。它也是w2k3,w2k8和xp的管理系統的控制核心),下面是完成這個工作的示範代碼:

       //////////////////////////////////////////////////////////////////////////////////////////////////

       //ConnectionOptions指定產生wmi串連所需的設定

       ConnectionOptions connOption = new ConnectionOptions();
       connOption.Username = domain + @"\" + userName;
       connOption.Password = password;

       //ManagementPath 封裝了產生和分析wmi對象的路徑

       ManagementPath mngPath = new ManagementPath(@"\\" + serverHostName + @"\root\cimv2:Win32_Process");
       ManagementScope scope = new ManagementScope(mngPath, connOption);
       scope.Connect();

       //ObjectGetOptions 類是指定用於擷取管理對象的選項

       ObjectGetOptions objOption = new ObjectGetOptions();

       //ManagementClass 是表示公用資訊模型 (CIM) 管理類,通過該類的成員,可以使用特定的 WMI 類別路徑訪問 WMI 資料
       ManagementClass classInstance = new ManagementClass(scope, mngPath, objOption);

       int ProcessId = 0;
       object[] cmdline = { "cmd /c " + strCommand, path, null, ProcessId };

       //調用執行命令的方法
       classInstance.InvokeMethod("Create", cmdline);

       其中domain是登陸遠程機的網域名稱,userName,password是登陸遠程機的帳戶密碼。

       serverHostName是要訪問的遠程機名或者IP。

       strCommand是需要在遠程機上面執行的命令。

     //////////////////////////////////////////////////////////////////////////////////////////////////

 

         c#中還可以通過使用 HTTP 協議傳輸訊息的用戶端通道,來實現遠程調用,下面是示範代碼:

         //首先建立通道,並註冊通道服務

            HttpChannel c = new HttpChannel();
            ChannelServices.RegisterChannel(c, false);

            //然後調用可執行檔執行操作

            object remoteObject = Activator.GetObject(Type.GetType(RemoteObject), remoteObjectURL);
            RemoteObject marshalObj = (RemoteObject)remoteObject;
            marshalObj.RunCommand(ExeFilePath);

            //關閉通道

            ChannelServices.UnregisterChannel(c);
            
            public class RemoteObject:MarshalByRefObject
            {
                public string RunCommand(string cmd)
                {
                    Process p=new Process();
                    p.StartInfo.FileName="cmd.exe";
                    p.StartInfo.UseShellExecute=false;
                    p.StartInfo.RedirectStandardInput=true;
                    p.StartInfo.RedirectStandardOutput=true;
                    p.StartInfo.RedirectStandardError=true;
                    p.StartInfo.CreateNoWindow=true;
                    p.Start();
                    p.StandardInput.WriteLine(cmd);
                    p.StandardInput.WriteLine("exit");
                    p.Close();

                }
            }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.