vbs automatic script - windows

Is it possible to create a vbs script that call itself when a certain pc action is activated,lets say opening up a browser?A replica would be someone opens up a browser,then the vbs listens to this activity and runs itself or calls another vbs script?

Jimmy, if you want the code to be executed when a browser window is opened, consider this code :
Set obj0 = createobject("wscript.shell")
Dim Count
Count = 0
Do while count = 0
If obj0.appactivate("browserwindowtitle") then
-------do something----
----
----
Count = 1
Else
Wscript.sleep(10000)
Count = 0
End if
Loop
Set obj0 = nothing
The above code will check if the window having the title "browserwindowtitle" is open or not. If it is open, it will execute the desired action. If the window is not open, script will wait for 10 seconds and try again.
After writing the script go to control panel-> scheduled tasks and add the script as scheduled on startup. When you do this, the script will start executing when your PC turns on and will keep on checking if the browser window is open or not.
There will be easier ways to do this, others might be able to help.

You may want to give System Scheduler a try.
http://www.splinterware.com/products/wincron.htm

Related

VBS & WSH error on simple loop and cpu over usage

I have a simple script that hunts for popup boxes that are generated for website and Excel.
It works most of the time but errors out occasionlly and seemingly randomly.
The error is
line: 6
Char: 3
Error: Invalid window handle
Code: 80070578
I can't figuer out why it'll work for hours then error seemingly at random.
Also the script uses a lot of CPU if anyone could advise how to make it more efficent.
Thanks For your time
Set WshShell = CreateObject("WScript.Shell")
Do
Do
ret = WshShell.AppActivate("Message from webpage")
ret2 = WshShell.AppActivate("Microsoft Excel")
Loop Until ret = True or ret2 = True
WScript.sleep 500
ret = WshShell.AppActivate("Message from webpage")
ret2 = WshShell.AppActivate("Microsoft Excel")
If ret = True or ret2 = True Then
WScript.Sleep 200
WshShell.SendKeys("{ENTER}")
End If
WScript.sleep 500
Loop
The simple answer is VBScript is not designed to run scripts continuously.
As for what you can do the improve it, let's dissect it a little.
The outer loop is running every 500 milliseconds, which means that every half a second the script is triggered forever this is heavy for what it is doing and will likely cause heavy CPU usage the longer it runs.
Does the outer loop need to run so often, could you not increase the Sleep() from 500 to say 1000 or even 5000 to reduce the load on the CPU?
Better yet you could drop the outer loop completely and run the task using Windows own Task Scheduler which will allow you to create a scheduled task that will execute the script however often you wish. That way the script runs to completion closes releases the memory and is then run again at the next interval.

Azure-Pipelines - Script that requires Ctrl+C or Typing 'y' to end

I'm using Azure-Pipelines for my CI Pipeline running on windows-2019. One of my cmd scripts generates output report files and continues to run until either Ctrl+C is pressed, or 'y' is typed. How is this done? If I add another script after this one as 'y', it will never reach because the previous command will never terminate.
In the meantime, I added the "timeoutInMinutes" param to the script so it timeouts in a minute, but this still causes a task error which is not ideal.
Does anyone have any ideas on how I can end the first script once it's complete? (It takes about 5 seconds to complete the necessary task)
You can't. it is impossible to enter a user input during Azure Pipeline.
You must edit your script to not finish and wait for user input.

How to restrict windows tasks to weekdays only?

I have a task that triggers on "user session logon". I now want to restrict that task to fire only on weekdays, and being ignored on the weekend.
Is that possible?
Sidenote: I cannot use the trigger on schedule as I do not want to run the task periodically, but only on logon, and only on weekdays.
click Weekly (NOT Daily)
Choose the days you want
As far as I understand, this is not possible using the task scheduler alone.
You could use a piece of VBScript to achieve this.
Set up a file, e.g. mytask.vbs, like this:
If DatePart("w", Date, vbMonday) < 6 Then
Set Shell = CreateObject("WScript.Shell")
WScript.Quit(Shell.Run("C:\Windows\System32\notepad.exe", 10, True))
End If
Replace notepad by the task you actually want to run. What this things does is: It checks whether the current day is Mo-Fr (this is done by specifying the start of the week as Monday, so DatePart will return values from 1=Monday to 7=Sunday, and then we're checking if it's below 6), and if yes, it runs a certain program, waits for it to finish and forwards its exit code. (The magic number 10 here means that it will respect whatever setting for window display (normal, maximized, minimzed) was passed by the task scheduler, if any, and also forward it to the program.)
Then you can create a scheduled task with a logon trigger only, which runs wscript.exe /e:vbscript c:\path\to\your\mytask.vbs. That's it!

Visual Basic: How to use timer properly

I'm trying to write a simple program that could perform some tasks at specified time.
Here's what I have:
If (TimeOfDay = "06:12:50") Then
MsgBox(TimeOfDay)
End If
If (TimeOfDay = "06:13:58") Then
MsgBox(TimeOfDay)
End If
This code is placed inside Timer1_Tick, I set time interval - 1000 and it works OK, I get TimeOfDay value in MsgBox when current time is equal to my specified time.
But what should I do to make it work dynamically? For example: I want to type TIME value via TextBox and pass it to Timer1_Tick I need to do it as many times as I want so everytime current time matches with my specified hour,minute,second it would work, but I don't know where I have to put my code, because if I place code in while loop and in Time_Ticker1 it runs while loop every second and UI crashes immediately.
Thank you in advance for your help!
Have you considered setting a Windows Scheduled event of MSG to yourself using the AT command line? The operating system timer/scheduler, dialog, storage and queue are already there and the MSG can optionally be dismissed if there is no one to receive it within a set amount of time. Example to send the time at 06:12:15 run the following into a command shell.
AT 06:12:15 msg %USERNAME% It is 06:12:15 am

How to get a list of running applications with VBS without using a call to word?

We are using the script below to kill any open windows that may interfere with the kiosk operation.
it uses word to return a list of running application not processes.
Problem is the script I have calls on Word and word keeps crashing. Seems like there should be a way to do this without the call to word.
here's the code I am using
sub closeWindows()
Set Word = CreateObject("Word.Application")
Set Tasks = Word.Tasks
For Each Task in Tasks
If Task.Visible AND Task.Name = "Sign Up for Facebook" Then Task.Close()
If Task.Visible AND Task.Name = "Log In | Facebook: Facebook" Then Task.Close()
Next
Word.Quit
end sub
Do
closeWindows
Wscript.Sleep 1000
Loop
There are two options preferable to the solution you are suggesting:
Lock down / harden the kiosk to prevent the use of unwanted applications.
If you use a "watchdog" script like this, populate the script with the known good applications, not the ones you do not want.
Both of these options are far more sustainable than keep a blacklist.

Resources