我正在使用WMI远程启动交互式进程(refer this)
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 am然后它将安排到9:31],但是我想在当前时间仅5秒后安排任务时间。我修改了以下几行:
来自:
Const INTERVAL = "n"
Const MINUTES = 1
致:
Const INTERVAL = "s"
Const SECONDS = 5
但它无法正常工作。我认为问题是当我们呼叫SetVarDate(DateAdd(INTERVAL, _
SECONDS, Now()))
SECONDS被添加到当前时间时,如果当前时间是9:30:20 AM,那么预定时间将是9:30:25 AM,但是如果时间当前时间是9 :30:57 AM然后问题。
请有人帮我解决这个问题吗?
提前致谢。
编辑:
为了调用上面的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))
。
如何解决这个问题?
答案 0 :(得分:2)
我不确定我是否正确理解你的问题,但听起来当地时间和远程机器上的时间之间存在差异。 您可能希望在远程计算机上获得时间,然后使用它而不是“现在”。
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_LocalTime")
For Each objItem in colItems
If objItem.Hour > 12 Then
intHour = objItem.Hour - 12
strAMPM = "PM"
Else
intHour = objItem.Hour
strAMPM = "AM"
End If
strTime = objItem.Month & "/" & objItem.Day & "/" & objItem.Year & " " _
& intHour & ":" & objItem.Minute & ":" & objItem.Second & " " & strAMPM
Next
然后你可以做
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL,MINUTES, strTime))