VB Script to Create a Scheduled Task on a Remote Machine Using Alternate Credentials

69605 pts.
Tags:
IT Scripts
MS Windows
Scheduled Tasks
VBScript
WMI
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)
Next
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
    Wscript.Echo "1"
Else
    Wscript.Echo "0"
End If
Feel free to post any comments/doubts/suggestions in the discussion section below.

Software/Hardware used:
MS Windows, WMI

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 10  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Mariodlg
    Carlosdl I am trying this script but I get the message Access is Denied in vbscript line 7, this is the function Locator.ConnectServer. I already checked the username and password and they are ok. I searched the web for any environment adjustment that I needed to make in the destination computer, but only I found this link, but I don't know if I am looking in the correct way. Any suggestions? Thanks
    2,790 pointsBadges:
    report
  • carlosdl
    Hi Mario. Can you share how you are calling the script ? Also, what is the local and remote machines' OS ?
    69,605 pointsBadges:
    report
  • Mariodlg
    Hi Carlos, here is the call .remoteappexec.vbs 172.16.10.205 mariodlg xxxxxxx c:WindowsSystem32notepad.exe I save the script as "remoteappexec.vbs" and "xxxxxxx" is the password for the user "mariodlg", I'm trying to schedule notepad.exe execution as test. Both machines have Windows 7 Professional installed. The error message is : Command sequence: C:Usersmariodlgscriptsremoteappexec.vbs Line: 7 Character: 2 Error: Access denied Code: 80070005 Source: SWbemLocator Thanks.
    2,790 pointsBadges:
    report
  • Mariodlg
    I forgot to put double backslash: .\remoteappexec.vbs 172.16.10.205 mariodlg xxxxxxx c:WindowsSystem32notepad.exe
    2,790 pointsBadges:
    report
  • carlosdl
    Try reviewing the WMI security configuration on the remote machine. -Run dcomcnfg to open the "Component Services" console -Expand "Component Services"->"Computers"->"My computer"->"DCOM config" -Look for the "Windows Management and Instrumentation" item, and right-click on it -Click on "Properties" and navigate to the "Security" tab. -Edit the "Launch and activation" permissions, and make sure the appropriate user or group has the remote launch and remote activation permissions allowed. -Edit the "Access" permissions and make sure the appropriate user or group has the remote access permission allowed. -Try again. Those permissions are the most common causes of "access denied" errors when using WMI. Third party security tools (firewalls, host intrusion prevention systems, etc) could cause issues as well. If the machine is part of a domain, I would also use <DOMAIN><username> to connect instead of just <username> (this may or may not make a difference).
    69,605 pointsBadges:
    report
  • carlosdl
    I forgot to escape the backslash too. I meant <DOMAIN>\<username>
    69,605 pointsBadges:
    report
  • carlosdl
    [...] that pointed to scripts he’d previously posted to help others out. Carlos actually posted several really useful scripts, making it hard to pick one, which is why we [...]
    0 pointsBadges:
    report
  • Mariodlg
    Hi Carlos. The remote machine had remote access disabled in WMI Configuration. Now your script is working, thanks a lot. Regards.
    2,790 pointsBadges:
    report
  • carlosdl
    Great. Thanks for posting back, Mario.
    69,605 pointsBadges:
    report
  • carlosdl
    [...] CarlosDL shares a savvy scheduling script, and others help him perfect [...]
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following