Schedule a .vbs file to run in Windows
-
08-07-2019 - |
Question
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
No correct solution
OTHER TIPS
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.
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
I'm pretty sure SendKeys will not work if the desktop is locked or no user is logged in.
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.
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.
Batch files don't work in Windows with Telnet (works fine in UNIX -- again, way to go Microsoft). As already mentioned here, sendkeys does not work in vba when not logged on.
Sorry I don't have the "this does work" solution for you....I'm stuck on the same problem