This is a small VB script I created to remotely schedule a task on another machine, using a different user account from the one logged in the local computer.
It was needed because PsExec (a sysinternals' tool we use a lot) didn't always work well in some situations where we needed to execute remote tasks on machines members of a different domain or workgroup, running different Windows versions.
It takes the remote machine ip address, the username and password and the remote process as arguments, and schedule its execution with a 1 minute delay, using the remote machine's time.
Here is the code:
'Arguments: <ip address> <username> <password> <program to run>
Const MINUTES = "n"
'Connects to the remote machine's WMI using the provided credentials
Set Locator = CreateObject("WbemScripting.SWbemLocator")
Set Service = Locator.ConnectServer(Wscript.Arguments(0), "rootcimv2", Wscript.Arguments(1), Wscript.Arguments(2))
Set oProcess = Service.Get("Win32_ScheduledJob")
'Gets the remote time, and converts it to the appropriate format
Set colItems = Service.ExecQuery("Select * From Win32_LocalTime")
For Each objItem in colItems
strTime = objItem.Hour & ":" & objItem.Minute & ":" & objItem.Second
dtmTime = CDate(strTime)
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.SetVarDate(DateAdd(MINUTES, 1, dtmTime ))
'Creates the task, and checks the results. Returns 1 if the task is created, 0 if the creation fails
errReturn = oProcess.Create(Wscript.Arguments(3), objSWbemDateTime.Value, False, 0, 0, True, intJobID)
If errReturn = 0 Then
Feel free to post any comments/doubts/suggestions in the discussion section below.