2013-10-04 69 views
0


我開始遠程使用WMI(refer this) 交互式過程
如何使用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 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))

如何解決這個問題?

回答

1

我不知道我是否正確理解您的問題,但聽起來像是本地時間和遠程計算機上的時間之間存在差異。 您可能想要在遠程計算機上獲得時間,然後使用該時間而不是「now」。

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)) 
+1

你可能會檢查這個代碼,以確保它在午夜,當objItem.Hour將等於0,我剛纔注意到這一點。 – langstrom

相關問題