Schedule a .vbs file to run in Windows

22,304

Solution 1

Add a scheduled task that runs the script with your credentials. Remind yourself that you need to update the credentials on the task every time you change your password. It be a good idea to have the script "phone home" via email or something every time it is run so that you can tell if it is being executed.

It might also be a good idea to set up a separate service id for these sorts of activities. You may not need to change the password on the service id as frequently.

Solution 2

You can add a scheduled task and enter no credentials or password for it. This will cause it to run under LOCAL SYSTEM (which normally is the context the Task Scheduler service uses).

Be aware that this is a backdoor vulnerability scenario: Anyone allowed to edit your script file could misuse it to do undesirable things on the machine that runs the task. Put proper permission on the script file to prevent that. On the other hand - a task running as LOCAL SYSTEM cannot wreck havoc over the network.

I propose you condense your script file a little:

Set WshShell = WScript.CreateObject("WScript.Shell") 

Run "cmd.exe" 
SendKeys "telnet 10.20.70.254{ENTER}" 
SendKeys "netscreen" 
SendKeys "{ENTER}" 
SendKeys "netscreen" 
SendKeys "{ENTER}" 
SendKeys "save conf to tftp 10.10.40.139 test.cfg{ENTER}"
SendKeys "exit{ENTER}" 'close telnet session' 

Run "cmd.exe" 
SendKeys "telnet 10.20.70.254{ENTER}" 
SendKeys "netscreen" 
SendKeys "{ENTER}" 
SendKeys "netscreen" 
SendKeys "{ENTER}" 
SendKeys "save conf to tftp 10.10.40.139 palsg140.cfg{ENTER}" 'repeat as needed 
SendKeys "exit{ENTER}" 'close telnet session' 
SendKeys "{ENTER}" 'get command prompt back 
SendKeys "exit{ENTER}" 'close cmd.exe
SendKeys "{ENTER}" 'get command prompt back 
SendKeys "exit{ENTER}" 'close cmd.exe

Sub SendKeys(s)
  WshShell.SendKeys s
  WScript.Sleep 300
End Sub

Sub Run(command)
  WshShell.Run command
  WScript.Sleep 100 
  WshShell.AppActivate command 
  WScript.Sleep 300 
End Sub

Solution 3

just make a batch file that contains this:

cscript.exe myscript.vbs

save it as something like myscript.bat.

Use schedule tasks to schedule the .bat file. After you create the scheduled task, you may have to check it's properties to make sure it's has appropriate user rights.

There are some options you can use with cscript so it doesn't show the logo, etc.

Solution 4

I'm pretty sure SendKeys will not work if the desktop is locked or no user is logged in.

Solution 5

I'm pretty SendKeys will not work if you aren't logged in. It's unreliable in my experience anyway. You might be better off using a DOS batch file.

getftpconf.bat:

telnet 10.10.40.139
netscreen
netscreen
save conf to tftp 10.10.40.139 palsg140.cf
exit

Something like that.

If there is output in the command prompt that you need to record, you can put a " >> output.txt" at the end of the command line shortcut.

You could then call another batch file which sends that output.txt via ftp to where ever you need.

You can easily setup this batch file to run as a scheduled task in windows.

Share:
22,304
Peter Mortensen
Author by

Peter Mortensen

Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (12 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly, CAN bus/CANopen. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com and possibly other(s)). Make sure to make the subject specific (I said: specific. Repeat: specific subject required). I can not stress this enough - 90% of you can not compose a specific subject, but instead use some generic subject. Use a specific subject, damn it! You still don't get it. It can't be that difficult to provide a specific subject to an email instead of a generic one. For example, including meta content like "quick question" is unhelpful. Concentrate on the actual subject. Did I say specific? I think I did. Let me repeat it just in case: use a specific subject in your email (otherwise it will no be opened at all). Selected questions, etc.: End-of-line identifier in VB.NET? How can I determine if a .NET assembly was built for x86 or x64? C++ reference - sample memmove The difference between + and & for joining strings in VB.NET Some of my other accounts: Careers. [/]. Super User (SU). [/]. Other My 15 minutes of fame on Super User My 15 minutes of fame in Denmark Blog. Sample: Jump the shark. LinkedIn @PeterMortensen (Twitter) Quora GitHub Full jump page (Last updated 2021-11-25)

Updated on July 06, 2022

Comments

  • Peter Mortensen
    Peter Mortensen almost 2 years

    I have a VBScript script that starts a cmd prompt, telnets into a device and TFTP's the configuration to a server. It works when I am logged in and run it manually. I would like to automate it with Windows Task Scheduler.

    Any assistance would be appreciated, here is the VBScript script:

    set WshShell = WScript.CreateObject("WScript.Shell") 
    WshShell.Run "cmd" 
    WScript.Sleep 100 
    WshShell.AppActivate "C:\Windows\system32\cmd.exe" 
    WScript.Sleep 300 
    WshShell.SendKeys "telnet 10.20.70.254{ENTER}" 
    WScript.Sleep 300 
    WshShell.SendKeys "netscreen" 
    WScript.Sleep 300 
    WshShell.SendKeys "{ENTER}" 
    WScript.Sleep 300
    WshShell.SendKeys "netscreen" 
    WshShell.SendKeys "{ENTER}" 
    WScript.Sleep 300 
    WScript.Sleep 300 
    WshShell.SendKeys "save conf to tftp 10.10.40.139 test.cfg{ENTER}"
    WScript.Sleep 200 
    WshShell.SendKeys "exit{ENTER}" 'close telnet session' 
    set WshShell = WScript.CreateObject("WScript.Shell") 
    WshShell.Run "cmd" 
    WScript.Sleep 100 
    WshShell.AppActivate "C:\Windows\system32\cmd.exe" 
    WScript.Sleep 300 
    WshShell.SendKeys "telnet 10.20.70.254{ENTER}" 
    WScript.Sleep 300 
    WshShell.SendKeys "netscreen" 
    WScript.Sleep 300 
    WshShell.SendKeys "{ENTER}" 
    WScript.Sleep 300
    WshShell.SendKeys "netscreen" 
    WshShell.SendKeys "{ENTER}" 
    WScript.Sleep 300 
    WScript.Sleep 300 
    WshShell.SendKeys "save conf to tftp 10.10.40.139 palsg140.cfg{ENTER}" 'repeat as needed 
    WScript.Sleep 200 
    WshShell.SendKeys "exit{ENTER}" 'close telnet session' 
    WshShell.SendKeys "{ENTER}" 'get command prompt back 
    WScript.Sleep 200 
    WshShell.SendKeys "exit{ENTER}" 'close cmd.exe
    
    WshShell.SendKeys "{ENTER}" 'get command prompt back 
    WScript.Sleep 200 
    WshShell.SendKeys "exit{ENTER}" 'close cmd.exe
    
    • Alexander Aleksandrovič Klimov
      Alexander Aleksandrovič Klimov over 15 years
      What happens when you try? You don't say what problems you are experiencing
  • Rabeel
    Rabeel over 15 years
    If telnet and cscript may need a desktop context to run in (especially with "sendkeys"). You may need to leave the user logged into the machine for the scheduled task to work correctly.