由于在win 7系统下,at 的方式建立的任务无法在前端显示。故采用schtasks的方式建立任务计划。
代码叫乱,主要是在RunRemoteCommand方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;
namespace kongzhi
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(RunRemoteCommand("ST.bat",@"D:\"));
//RemoteService service = new RemoteService();
//service.startService();
Console.WriteLine("按任意键结束..");
Console.ReadKey();
}
private static bool RunRemoteCommand(string strCommand, string path)
{
try
{
ConnectionOptions connOption = new ConnectionOptions();
connOption.Username = "ywx143107";
connOption.Password = "ye3+2-5*0=?";
ManagementPath mngPath = new ManagementPath(@"//" + "10.169.38.133" +
@"/root/cimv2:Win32_Process");
ManagementScope scope = new ManagementScope(mngPath, connOption);
scope.Connect();
ObjectGetOptions objOption = new ObjectGetOptions();
ManagementClass classInstance = new ManagementClass(scope, mngPath, objOption);
ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
int ProcessId = 0;
//inParams.SetPropertyValue("CommandLine", strCommand);
//object[] cmdline = { "cmd /c " + strCommand, path, null, ProcessId };
//schtasks /delete /F /TN idesk3 强制删除计划任务idesk3
object[] cmdline = {"cmd /c schtasks /create /tn idesk3 /tr D:\\ST.bat /sc once /st 14:31" };
classInstance.InvokeMethod("Create", cmdline);
return true;
}
catch (Exception)
{
return false;
}
}
public static string RunCommand1(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 = false;
p.Start();
p.StandardInput.WriteLine(cmd);
p.StandardInput.WriteLine("exit");
string message = p.StandardOutput.ReadToEnd();
p.Close();
return message;
}
}
class RemoteService {
private string strPath;
private ManagementClass managementClass;
public void startService()
{
if (remoteConnectValidate("192.168.1.100", "********", "********"))
{
win32ServiceManager("192.168.1.101", "********", "********");
startCmd(managementClass);
execBat(managementClass);
//inParams["CommandLine"] = "D:\\ST.bat";
// object[] cmdline = { "cmd /c " + "ST.ba", "D:/", null,0 };
//object outParams= managementClass.InvokeMethod("Create", cmdline);
//ManagementBaseObject outParams = managementClass.InvokeMethod("Create", inParams, null);
// Console.WriteLine("Creation of calculator process returned: " + outParams);
//Console.WriteLine("Process ID: " + outParams);
//string[,] services = getServiceList();
//for (int i = 0; i < services.GetLength(0); i++)
//{
// Console.WriteLine(services[i, 0]);
// Console.WriteLine(services[i, 1]);
// Console.WriteLine(services[i, 2]);
// Console.WriteLine(services[i, 3]);
// Console.WriteLine(Environment.NewLine);
//}
//string serviceName = "AdobeARMservice";
//ManagementObject mo = this.managementClass.CreateInstance();
//mo.Path = new ManagementPath(this.strPath + ".Name=\"" + serviceName + "\"");
//StartService 启动服务,PauseService 暂停服务,ResumeService 恢复服务,StopService 停止服务
//mo.InvokeMethod("StartService", null);
}
}
/// <summary>
/// 启动cmd
/// </summary>
/// <param name="managementClass"></param>
/// <returns></returns>
public void startCmd(ManagementClass managementClass)
{
ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
//Fill in input parameter values
inParams["CommandLine"] = "cmd.exe";
//Execute the method
ManagementBaseObject outParams = managementClass.InvokeMethod("Create", inParams, null);
//Display results
//Note: The return code of the method is provided in the "returnvalue" property of the outParams object
Console.WriteLine("Creation of calculator process returned: " + outParams["returnvalue"]);
Console.WriteLine("Process ID: " + outParams["processId"]);
}
/// <summary>
/// 验证是否能连接到远程计算机
/// </summary>
/// <param name="host">主机名或ip</param>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns></returns>
public static bool remoteConnectValidate(string host, string userName, string password)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
// connectionOptions.Username = userName;
//connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope("\\\\" + host + "\\root\\cimv2", connectionOptions);
try
{
managementScope.Connect();
}
catch
{
}
return managementScope.IsConnected;
}
/// <summary>
/// 获取所连接的计算机的所有服务数据
/// </summary>
/// <returns></returns>
public string[,] getServiceList()
{
string[,] services = new string[this.managementClass.GetInstances().Count, 4];
int i = 0;
foreach (ManagementObject mo in this.managementClass.GetInstances())
{
services[i, 0] = (string)mo["Name"];
services[i, 1] = (string)mo["DisplayName"];
services[i, 2] = (string)mo["State"];
services[i, 3] = (string)mo["StartMode"];
i++;
}
return services;
}
public void win32ServiceManager(string host, string userName, string password)
{
//this.strPath = "\\\\" + host + "\\root\\cimv2:Win32_Service";
this.strPath = "\\\\" + host + "\\root\\cimv2:Win32_Process";
this.managementClass = new ManagementClass(strPath);
if (userName != null && userName.Length > 0)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope("\\\\" + host + "\\root\\cimv2", connectionOptions);
this.managementClass.Scope = managementScope;
}
}
}
}