Schedule a .vbs file to run in Windows - command-line

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

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

Related

VBScript sendkeys doesn't work on powershell in admin mode

I have a vbscript that spawns powershell in admin mode via:
Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "powershell", "-executionpolicy bypass", "", "runas", 1
and then I attempt to send keys via:
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.sleep 2000
WshShell.sendkeys "hello"
Since the keys didn't work, I tried separating the latter part into a separate file and specifically did a WshShell.AppActivate on the PID to make sure it gets the right window focus before sendkeys, however it still won't send the string.
Conversely, if I don't run powershell in admin, everything works fine:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.run "powershell"
WScript.sleep 200
WshShell.sendkeys "cls"
Can someone please tell me what I'm doing wrong?
While I don't have an official reference for you, I'm pretty sure that by design, for security reasons, you can not send keystrokes to elevated (run-as-admin) processes from a non-elevated process.
If this were possible, non-elevated processes could bypass their limited privileges, which would be an obvious violation of the rules.

Wscript .vbs file not running in task scheduler in windows 10

I have a wscript to run a .vbs file on my desktop. And I want it to run every day at a certain time. I set up a task in the Windows 10 to run the script, but nothing happens at the designated time. Under the "action" tab in the scheduler I have wscript.exe in the Program/Script: input line, and the file path in the Add Arguments line. When I simply double click on the .vbs file on my desktop, it works just fine, but for some reason the task scheduler won't start it, even when I try clicking "Run" in the scheduler to test. I've been reading on some forums that maybe it needs to be a cscript.exe, but I've tried that it doesn't work. My .vbs file does use wscirpt, so maybe I need to change the script to cscript at the same time and rerun it. I have tried that, but it doesn't seem to work. Below the script in my .vbs file.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """%PROGRAMFILES(x86)%\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"""
WScript.Sleep 3000
WshShell.AppActivate "Cisco AnyConnect Secure Mobility Client"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 5000
WshShell.SendKeys "XXXXX"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 5000
WshShell.SendKeys "{ENTER}"
WScript.Sleep 5000
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\carls\Desktop\REIT Model\Model Forward\Stock Model REITs.xlsm")
objExcel.Application.Visible = True
objExcel.Application.Run "PortfolioEvaluator"
Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run "outlook"
WScript.Sleep 30000
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """%PROGRAMFILES(x86)%\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"""
WScript.Sleep 3000
WshShell.AppActivate "Cisco AnyConnect Secure Mobility Client"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"

Batch - minimize window while running a loop command (not start minimized)

I'm wondering if there is a way to minimize a batch window after it runs a certain command. I already know start /min and tricks to START the window minimized but what about while it's running a loop or timeout?
Let's say:
echo Hello!
timeout /t 100
:COMMAND TO MINIMIZE WINDOW WHILE TIMEOUT IS RUNNING
Right now i'm calling an autoit script in the bat file to hide the window while the command is running with :
WinSetState($application_name, "", #SW_HIDE)
but i'm looking for a pure batch/powershell/vbs solution that can be coded directly in the .bat file.
Thank you for your time!
Use PowerShell's invocation options, executing no command or script.
#echo off & setlocal
echo Hello!
powershell -window minimized -command ""
timeout /t 100
powershell -window normal -command ""
FWIW, -window hidden is also available if you wish.
You can also minimize all windows by using below code with powershell.
$shell = New-Object -ComObject "Shell.Application"
$shell.minimizeall()
Check:
https://techibee.com/powershell/powershell-minimize-all-windows/1017
You can minimize the command prompt on during the run but you'll need two additional scripts: windowMode and getCmdPid.bat:
#echo off
echo Hello!
call getCmdPid >nul
call windowMode -pid %errorlevel% -mode minimized
timeout /t 100
call getCmdPid >nul
call windowMode -pid %errorlevel% -mode normal
This will do. You need to however run the minimize before the timeout as it is in batch. Timeout will now occur once the window is minimized. This example will keep the window during the ping so you can see it minimizes.
echo Hello!
ping 127.0.0.1
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
timeout /t 100
exit

Protractor supports Windows Authentication pop up?

Is there a way that Protractor supports windows authentication pop up for IE and Chrome, this doesn't work for my application, "http://username:password#server.url/", are there any alternate ways through which we can handle windows pop up.
Chrome supports directly embedding credentials in URL and just works fine as "http://username:password#server.url/".
However IE does not support that as security issue so you may have to write a shell script as below:
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Windows Security"
WshShell.Sleep 500
WshShell.SendKeys "Username"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "password"
WshShell.SendKeys "{ENTER}"

CMD - openning app as user

I'm trying to lauch outlook with CMD with simple script I have made. Problem becomes when I open this script on user's computer via CMD ran as admin (my user account) it will open me outlook as I would be opening it now user. So my inbox will apper not user. Script works perfectly if I just run it from normal cmd not elevated mode.
How do I make sure that from elevated CMD it will open me user's inbox not user's that has elvated CMD?
strPath = WshShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%")
outlook15 = strPath & "\Microsoft Office\Office15\outlook.exe"
outlook14 = strPath & "\Microsoft Office\Office14\outlook.exe"
If fso.FileExists(outlook15) Then
msgbox "TITUS successfully fixed!", 64
WScript.Sleep 3000
WshShell.Run Chr(34) & outlook15 & Chr(34)
Set fso = Nothing
Set WshShell = Nothing
Else
msgbox "TITUS successfully fixed!", 64
WScript.Sleep 3000
WshShell.Run Chr(34) & outlook14 & Chr(34)
Set fso = Nothing
Set WshShell = Nothing
End If
To detect the current user running CMD you can grab the output from the whoami command like this:
Set oExec = WshShell.Exec("whoami")
strUser = oExec.StdOut.ReadLine
Then you can add runas to the WshShell.Run statement to change the user context:
If strUser = "admin" Then
WshShell.Run "runas /user:" & otherUser & " " & Chr(34) & outlook15 & Chr(34)
End If
The other username will need to be supplied, either through an input prompt, or it can be captured from another command(qwinsta, psloggedon, etc) and extracted via regex. It also should be noted that runas will prompt for the other user's password.