我正在使用 WMI 远程启动交互式进程(请参阅此)
Const INTERVAL = "n"
Const MINUTES = 1
strComputer = "machineDomain"
strCommand = "Notepad.exe"
Set objWMIService = _
GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob")
Set objSWbemDateTime = _
CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, _
MINUTES, Now()))
intReturnValue = objScheduledJob.Create(strCommand, _
objSWbemDateTime.Value, False, 0, 0, True, intJobID)
WScript.Echo "Job ID: " & intJobID
问题在上面的代码中是任务将安排到下一分钟[意味着如果当前时间是上午 9:30,那么它将安排到 9:31],但我想在当前时间后 5 秒安排任务。我修改了以下几行:
来自:
Const INTERVAL = "n"
Const MINUTES = 1
至 :
Const INTERVAL = "s"
Const SECONDS = 5
但它不能正常工作。我认为问题是当我们调用SetVarDate(DateAdd(INTERVAL, _
SECONDS, Now()))
SECONDS 时被添加到当前时间,例如如果当前时间是上午 9:30:20,那么计划时间将是上午 9:30:25,但是如果时间当前时间是上午 9:30:57然后它就会出现问题。
请问谁能帮我解决这个问题?
提前致谢。
编辑:
为了在我使用以下代码[^]调用上述 vbscript 代码:
string remoteMachine = Console.ReadLine();
string sBatFile = string.Empty;
if (remoteMachine != string.Empty)
sBatFile = @"\\" + remoteMachine + "\\admin$\\process.bat";
else
Console.WriteLine("Invalid Machine name");
if (File.Exists(sBatFile))
File.Delete(sBatFile);
StreamWriter sw = new StreamWriter(sBatFile);
string _cmd = "DIR > \\\\" + remoteMachine + "\\admin$\\output.txt";
Console.Write("Enter the remote Command <eg : Notepad.exe, Dir, Shutdown - r, etc..> : ");
_cmd = Console.ReadLine();
if ( _cmd.Trim()==string.Empty )
Console.WriteLine("No command entered using default command for test :" + _cmd);
sw.WriteLine(_cmd);
sw.Close();
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", remoteMachine), connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["CommandLine"] = sBatFile;
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
Console.WriteLine("Creation of the process returned: " + outParams["returnValue"]);
Console.WriteLine("Process ID: " + outParams["processId"]);
它工作正常,但经过一些尝试后它开始给出异常:
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
。
如何解决这个问题呢 ?