Search code examples
windowsvbscript

Start an Interactive Process Remotely just after creating scheduled task


I want to remotely start a script, which needs to run in the users access like this:

https://devblogs.microsoft.com/scripting/how-can-i-remotely-start-an-interactive-process/

because its not possible otherwise for security reasons like described in this article.

this is the script that's works like i wanted:


Const OverwriteExisting = TRUE

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
objFSO.CopyFile “C:\Scripts\Interactive.vbs”, _
    “\\atl-ws-01\C$\Scripts\”, OverWriteExisting
strComputer = “atl-ws-01”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set objNewJob = objWMIService.Get(“Win32_ScheduledJob”)
errJobCreated = objNewJob.Create _
    (“wscript.exe c:\scripts\interactive.vbs”, “********084500.000000-420”, _
        False, 2, , True, JobID)

but how can i run this right after i create the task? this runs at certain time, i want just only when i clicked in the script and create the task...

i just want to click, start script and nothing more...

just want to change this “********084500.000000-420” to now.

New Version of the Code:

    Const OverwriteExisting = TRUE
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "C:\Users\lucas.b\Desktop\teste.bat", _
        "C:\Users\lucas.b\Desktop\atuweb\", OverWriteExisting
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
    Function GetTimeString(p_iDelay)
        GetTimeString = "********" & Right("0" & Hour(Now), 2) & Right("0" & Minute(Now) + p_iDelay, 2) & "00.000000-180"
    End Function
    errJobCreated = objNewJob.Create _
    ("wscript.exe C:\Users\lucas.b\Desktop\teste.bat", GetTimeString(2), _
     False, 0, 0, True, JobID)
    If errJobCreated <> 0 Then
    Wscript.Echo "error on task creation"
    Else
    Wscript.Echo "Task created"
    End If

but the problem right know its because Win32_ScheduledJob its deprecated:

https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-scheduledjob

and even i creating the register like this:

stackoverflowschedulejob

its not working, dont create the schedulejob.


Solution

  • This function will generate an updated time string using your machine's clock. This assumes the remote machine is in the same time zone as yours and that its clock is (mostly) synchronized with yours. You can add a one or two minute delay to factor in clock differences (and issues with triggering the script on the 59th second of a minute and it executing in the next minute on the remote machine):

    Function GetTimeString(p_iDelay)
        GetTimeString = "********" & Right("0" & Hour(Now), 2) & Right("0" & Minute(Now) + p_iDelay, 2) & "00.000000-420”"
    End Function
    

    You would use it like this with your existing code:

    errJobCreated = objNewJob.Create _
        (“wscript.exe c:\scripts\interactive.vbs”, GetTimeString(2), _
            False, 2, , True, JobID)