日期:2014-06-10  浏览次数:20394 次

由于业务需要,需要批量远程服务器,启动对应的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"]);

}
}
}