简体   繁体   English

如何使用WMI远程启动Interactive Process?

[英]How to start Interactive Processes Remotely using WMI?


i am starting an interactive process remotely using 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

the problem is in above code is the task will schedule to next minute [means if current time is 9:30 am then it will schedule to 9:31], but i want to schedule the task just 5 second after the current time. 问题在上面的代码中是任务将安排到下一分钟[意味着如果当前时间是9:30,那么它将安排到9:31],但是我想将任务安排在当前时间之后5秒。 I modified the following lines: 我修改了以下几行:
From: 从:

Const INTERVAL = "n"
Const MINUTES  = 1

To : 至 :

Const INTERVAL = "s"
Const SECONDS  = 5

but it's not working correctly. 但它无法正常工作。 I think the problem is when we call SetVarDate(DateAdd(INTERVAL, _ SECONDS, Now())) SECONDS is get added to current time like if current time is 9:30:20 AM then schedule time will be 9:30:25 AM, but if time current time is 9:30:57 AM then it makes problem. 我认为问题在于,当我们调用SetVarDate(DateAdd(INTERVAL, _ SECONDS, Now())) SECONDS被添加到当前时间时,例如如果当前时间是9:30:20 AM,则计划时间将是9:30:25 AM,但是如果当前时间是9:30:57 AM,则会出现问题。

Please can anyone help me to solve this problem? 请谁能帮助我解决这个问题?

Thanks in Advance. 提前致谢。

EDIT : 编辑:
For invoking the above vbscript code in i am using the following code [^] : 为了在我中调用上述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"]);

it working fine but after some try it start giving exception : 它工作正常,但经过一些尝试后,它开始给出异常:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) . Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

how to solve this problem ? 如何解决这个问题呢 ?

I'm not sure if I'm understanding your question properly, but it sounds like there's a discrepancy between local time and the time on the remote machine. 我不确定我是否能正确理解您的问题,但听起来本地时间与远程计算机上的时间之间存在差异。 You might want to get the time on the remote machine, then use that instead of "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

Then you can do 那你可以做

objSWbemDateTime.SetVarDate(DateAdd(INTERVAL,MINUTES, strTime))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM