C#使用WMI在远程服务器上安排任务

时间:2013-10-15 14:15:09

标签: c# .net wmi scheduled-tasks windows-server-2008-x64

我尝试在远程Windows 2008服务器上运行批处理文件。我需要以交互方式运行该文件,因为它将打开许多交互式程序。我无法在远程服务器上安装服务。我在尝试在远程服务器上创建任务时遇到了困难

代码:

string command = @"c:\test.bat > c:\tmp_dr.dat";
ConnectionOptions conO = new ConnectionOptions();
conO.Username = username;
conO.Password = password;

ManagementScope scope = new ManagementScope(@"\\10.12.62.205\root\cimv2", conO);
scope.Connect();

//Getting time
string serverTime = null;
SelectQuery timeQuery = new SelectQuery(@"select LocalDateTime from Win32_OperatingSystem");
ManagementObjectSearcher timeQuerySearcher = new ManagementObjectSearcher(timeQuery);
foreach (ManagementObject mo in timeQuerySearcher.Get())
{
    serverTime = mo["LocalDateTime"].ToString();
}

//Adding 2 minutes to the time
string addTwoMinutes = (Convert.ToInt32(Strings.Mid(serverTime, 9, 4)) + 1).ToString();
if (Microsoft.VisualBasic.Strings.Mid(serverTime, 9, 1).Equals("0"))//Check if hour is zero
{
    addTwoMinutes = "0" + addTwoMinutes;
}
serverTime = Strings.Replace(serverTime, (Strings.Mid(serverTime, 9, 4)), addTwoMinutes, 1, -1, CompareMethod.Text);

//running command
object[] cmdParams = { command, serverTime, false, null, null, true, 0};
ManagementClass serverCommand = new ManagementClass(scope, new ManagementPath("Win32_ScheduledJob"), null);
serverCommand.InvokeMethod("Create", cmdParams);

我能够检索时间,但这似乎不会在远程服务器上创建任务。我对这一切都很陌生,所以我很欣赏人们可以提供的有关此事的任何指导。感谢

另外:psexec是不可能的。

解决方案:

命令变量作为字符串[]发送给AT。愚蠢的错误。

0 个答案:

没有答案