程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> wmi C# 遠程啟動exe文件,有界面

wmi C# 遠程啟動exe文件,有界面

編輯:C#入門知識

由於業務需要,需要批量遠程服務器,啟動對應的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(@"\\" + "IP地址" + @"\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);

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"]);

}
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved