Schedule a .vbs file to run on Windows

I have a VBScript script that runs a cmd prompt, telnets to the device, and TFTP setup to the server. It works when I am logged in and start it manually. I would like to automate it using Windows Task Scheduler .

Any help 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

      

+1


source to share


6 answers


Add a scheduled task that runs the script with your credentials. Remind yourself that you need to update your credentials for every task every time you change your password. It's a good idea to have a home phone script by email or something on every run so you can tell if it's running.



It might also be a good idea to create a separate service ID for these activities. You may not need to change the password on the Service ID frequently.

+4


source


You can add a scheduled task and not enter any credentials or password for it. This will cause it to run in LOCAL SYSTEM (usually the context used by the Task Scheduler service).

Be aware that this is a backdoor vulnerability scenario: anyone who can edit your script file could misuse it to do unwanted things on the machine that runs the task. Grant the correct permission for the script file to prevent this. On the other hand, a task executed as LOCAL SYSTEM cannot wreak havoc across the network.



I suggest configuring your script file a bit:

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

+3


source


I'm pretty sure SendKeys won't work if the desktop is locked or the user isn't logged in.

+1


source


I pretty much SendKeys won't work if you're not logged in. It is not reliable 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 on the command line that you want to write, you can put "→ output.txt" at the end of the command line shortcut.

Then you can call another batch file that sends this output.txt file via ftp to wherever you need it.

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

+1


source


just create a batch file containing this:

cscript.exe myscript.vbs

      

save it as something like myscript.bat.

Use scheduling tasks to schedule a .bat file. After creating a scheduled task, you might have to check its properties to ensure that it has the appropriate user rights.

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

+1


source


Batch files don't work on Windows with Telnet (works fine on UNIX - again to go to Microsoft). As mentioned here sendkeys does not work in vba when not logged in.

Sorry, I have no "this work" solution for you .... I am stuck with the same problem

0


source







All Articles