How do I create a .bat (batch) file that presses Enter key every 10800 seconds in a loop? - windows

I've tried to make it myself, but it didn't work, as I'm not experienced enough with .bat to get it working.
So what I need is bascily a .bat file which I only need to click once and it will "simulate" me clicking the key 'Enter' every 10800 seconds (3 hours) in a loop, so that it doesn't ever stop clicking the key 'Enter' with a loop time of 10800 seconds, would someone help me out please! :)

You can do this with VBScript and the WScript host:
Save this as "press_enter.vbs" and run it from the CLI with "wscript press_enter.vbs"
Dim WshShell, FSO, secs
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
secs = 10800
kill_file = "C:\Users\Public\Documents\kill_switch.txt"
Do While true
WScript.Sleep(secs * 1000) ' this is in milliseconds, multiple by 1000
WshShell.SendKeys "{ENTER}"
if FSO.FileExists(kill_file) then exit do
Loop
As you probably guessed, the program runs forever until it detects the existence of the file "C:\Users\Public\Documents\kill_switch.txt"
You can also kill the program by opening Task Manager and killing the "Microsoft Windows Based Script Host" which will be listed under the "Background processes" section...
Remember that if you stop the application with the "kill_switch.txt" file, you will need to remove it before you start the application again. Perhaps it is best just to kill it with Task Manager...

Related

How to write a script to automate Putty (PLINK) on Windows 10

I am doing a manual process that is done each day within Putty and wanted to automate it. There is no need for a person to do this because all of the keyboard inputs do not change each day. I'm trying to free up time to increase productivity, not to mention it is mind-numbing to continually do this every day. The process requires someone to open Putty (this would use plink of course), login (storing the password and username in plain text is fine, steps for generating a key are not necessary), enter the same keyboard presses, output the file manually, and then save it to a network drive folder. So this is a completely unnecessary process to have someone doing it manually and I am seeking a way to complete this automation.
Currently, this is being used in a Windows 10 environment and from what I have read, Putty (plink) is the best route to go. I can utilize other SSH programs if there is a better method as well, but I think this may be the better route from the research I've done. I haven't scripted much at all and I'm trying to learn a bit as I go with this. I need to automate logging into PLINK (done) and then multiple keyboard entries (kind of done) for each screen within the server I'm accessing. Essentially, each screen needs to enter predetermined keyboard keys, such as "ENTER", some numbers 1-10 depending on the screen and then when it is complete, I need to print the results to a file, which preferably would be a xlsx, but csv or text would suffice as well.
I have added the code that I was able to create so far below. I am stuck at this point because PLINK does not remain visible so it's tough to analyze the issue and the cursor keeps jumping to any active window when running it.
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "C:\PuTTY\plink.exe 123.server.com -l username -pw password -t{ENTER}"
WScript.Sleep 3000
WshShell.AppActivate "plink.exe"
WScript.Sleep 2000
WshShell.AppActivate "plink.exe"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 6000
WshShell.AppActivate "plink.exe"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 6000
WshShell.AppActivate "plink.exe"
WshShell.SendKeys "command123{ENTER}"
Update
I attempted to use the suggested code and it does work in terms of automating the login and bringing up the application within the server, but it is not accepting any of the inputs I attempt to code. Every time an input is used the following message below is displayed. Please note, my command I'm passing is 100% correct, it just seems to not even consider it. The script does actually input the text into the proper field, but it just errors it out.
Enter program name (or abbreviation): I don't recognize
that program name. Press <return> for a list of programs."
(I have updated code per suggestions).
(
echo WEST COAST
timeout /t 5 > nul
echo 09
timeout /t 5 > nul
echo third_screen_keys
) | C:\PuTTY\plink.exe 123.server.com:PORT# -l username -pw password -t
There's a rather similar question here:
Wait between sending login and commands to serial port using Plink
So you should be able to use a batch file like this:
(
echo first_screen_keys
timeout /t 5 > nul
echo second_screen_keys
timeout /t 5 > nul
echo third_screen_keys
) | C:\PuTTY\plink.exe 123.server.com -l username -pw password -t
(or the PowerShell equivalent, if you *nix CR line endings)

Get possible syntax error when calling a VBScript without waiting till the end

I'm currently starting a VBScript from another VBScript via WScript.Exec without waiting for its result (which is working just fine):
Set ObjShell = CreateObject("WScript.Shell")
Set ShellExec = ObjShell.Exec(strFinalCommand)
Set ShellExec = Nothing
Set ObjShell = Nothing
I've just discovered a problem if the second script (which has its own error handling inside) does have a syntax issue.
Since this is determined before the script starts running, I can't notice the error in the second script but instead have to handle it in the first one.
Is there any possibility to check if the call threw an error at the start and - if not, just let it work
- if there is an error, stop the wscript and get the error text.
I haven't found anything on such a specific topic so far.
I've already tried ShellExec.ExitCode and ShellExec.StdErr, but are both are only updated after the second script finishes.
If there is an error, both are not updated.
I also tried ShellExec.SdtErr.AtEndOfStream, which crashed my process if an error appeared.
Edit:
The Command I'm running is wscript.exe D:\\...\RunAsyncShell.vbs /ParamOne:123 /ParamTwo:ABC with about 10 params more to follow (can't show the exact call due to company regulations).
You could do something like this:
Set ObjShell = CreateObject("WScript.Shell")
Set ShellExec = ObjShell.Exec(strFinalCommand)
limit = DateAdd("s", 2, Now)
Do While ShellExec.Status = 0 And Now < limit
WScript.Sleep 100
Loop
If ShellExec.ExitCode <> 0 Then
WScript.Echo "Script failed."
End If
That will wait up to 2 seconds after launching the second script, and report an error if the process returned a non-zero exit code within that time frame. However, you'll probably have to run the second script with cscript.exe instead of wscript.exe, because the latter will display a popup when a syntax error occurs, and the process will not terminate before you click that popup away.

Wait until program starts

I was wondering if there is any better way to wait until program starts before interacting with it?
Right now I'm using sleep which isn't exactly great.
Just to be clear I don't want to wait until program is finished (terminated) just to wait until it starts to be able to interact with it.
my code:
set MyShell = WScript.CreateObject("WScript.Shell")
MyShell.Run "<path to my exe app>"
WScript.Sleep 4000
MyShell.AppActivate "<my app name>"
MyShell.SendKeys "{TAB}"
...

How to write a VB task script that checks when a file is no longer being used?

Is there an event that triggers when a particular file, which was being used by some process, is no longer being used? If there is no event, is there another way to detect this in a way that can trigger a task?
Try this code:
Dim file
Dim app
file = "C:\Test\file.xlsx"
app = "notepad.exe"
On Error Resume Next
Do
CreateObject("Scripting.FileSystemObject").MoveFile file, file
If Err <> 70 Then Exit Do ' 70 - Access denied
Err.Clear
WScript.Sleep 1
Loop
CreateObject("WScript.Shell").Run app
Once being launched the script waits until the file is free, then runs the app and exits.

.vbs to stop .bat being run past a certain time

I have a scheduled task which runs on my server to put 2 other desktop machines asleep at 2am.
I also have a .bat file scheduled to run at 1:40am on all of my systems to let me know of the impending shutdown operation and give me the option to cancel, standby immediately or close the window and allow it to proceed. Works fine on all systems except a Win7 Laptop.
Despite me having the task set not to run past it's scheduled time, if I open my laptop and it resumes from standby any time after 2am, the file will still run.
I wanted to try and create a workaround by scheduling a .vbs to launch the .bat instead. Along the lines of:
if Hour(Time) > 1 Then
wscript.quit
else if Hour(Time) = 1 Then
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cancel-confirm-autostandby.bat C:\WINDOWS\system32\cmd.exe", 1
end if
wscript.quit
But the .bat file runs regardless, even testing now at 3am. If I try;
msgbox Hour(Time)
It returns a value of 3, so I don't understand this behavior. 3 is > than 1...
I have tried assigning the cutoff time (2am) to a varible tried Hour(Now) & Hour(Date).
Any suggestions welcome, thank you for reading...
Seem to have got it with
if Hour(Time) > 1 Then
wscript.quit
else if Hour(Time) = 1 Then
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cancel-confirm-autostandby.bat C:\WINDOWS\system32\cmd.exe", 1
end if
wscript.quit
End if
Don't know why it wouldn't work with just the second last "End if", but there you go...

Resources