由于業務需要,需要批量遠程服務器,啟動對應的exe文件。我最初也只是做到了啟動的文件在進程中,不能界面顯示,通過請教運維,通過任務計劃能啟動帶界面的程序,注意計算機需要開啟遠程桌面,注意防火墻,我是直接把火墻關閉了,網上說例外135端口也可,沒有試驗,下面是我test的一個demo。
直接上代碼吧:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Management;using System.Management.Instrumentation;
namespace test1{ class windowsWmi {
public void exStartCommand() { //ConnectionOptions指定生成wmi連接所需的設置 string userName = "用戶名"; string passWord = "密碼"; ConnectionOptions connOption = new ConnectionOptions(); connOption.Username = userName; connOption.Password = password; //ManagementPath包裝了生成和分析wmi對象的路徑 ManagementPath mngPath = new ManagementPath(@"http://" + " ManagementBaSEObject inParams = classInstance.GetMethodParameters("Create"); // Fill in input parameter values //inParams["CommandLine"] = @"D:/Program/UserAnts20120530/UserAnts20120530/UserAnts3/TaskWorker.exe";//只能啟動進程 inParams["CommandLine"] = "schtasks /run /tn /"Start03/""; //其中Start03是任務計劃的名稱,需要建立啟動exe的計劃任務 // Method Options InvokeMethodOptions methodOptions = new InvokeMethodOptions(null, System.TimeSpan.MaxValue); // Execute the method ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, methodOptions); //Console.WriteLine("Creation of calculator process returned: " + outParams["returnValue"]); //Console.WriteLine("Process ID: " + outParams["processId"]); } }}
新聞熱點
疑難解答