简体   繁体   中英

How to start Interactive Processes Remotely using WMI?


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

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. 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.

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 [^] :

            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)) .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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