0

I have a .vbs-Script that is running in the background endlessly. This script does not have any way for the user to interact with it; a regular user won't even notice something is running in the background. Now I need to be able to end this Script at any time.

So, now I'm searching for the easiest way to be able to cancel this .vbs-Script without using the Task Manager, other Tools or additional Scripts and without typing something like this to PowerShell or the Command Prompt:

PowerShell: Stop-Process -Name wscript
Command Prompt: Taskkill /IM wscript

I tried running a .cmd-Script that simply runs the .vbs-Script. I hoped I'd be able to end the .vbs-Script alongside with the .cmd either by pressing CTRL+C or by closing the Command Prompt, but I've tested it, and it didn't work this way.

So I googled a little bit and found out it's possible to create a visible Tray-Icon for a running .vbs-Script that would allow right-clicking it to close it. But I can't get it to work. Following you can see the code, I tried. The first three lines are for the Tray-Icon, line four to nine is the actual script that is running in the background:

Public Declare Function Shell_NotifyIcon Lib "shell32" _
  Alias "Caffeinate" _
  (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Dim objResult
Set objShell = WScript.CreateObject("WScript.Shell")    
Do While True
  objResult = objShell.sendkeys("{NUMLOCK}{NUMLOCK}")
  Wscript.Sleep (6000)
Loop

Does someone know how to make use of a System Tray-Icon for a vbs.Script?

Alternatively, any other "easy-to-use" way to cancel a .vbs-Script like that would be super nice.

Kind regards,

Kevin

  • 1
    You can't call Windows APIs directly from VBScript, so your Shell_NotifyIcon approach isn't going to work. See stackoverflow.com/questions/41516696/…, vbforums.com/showthread.php?526225-VBScript-tray-icon, and social.technet.microsoft.com/Forums/en-US/…. Why do you not want to use PowerShell? If you can describe why you are restricted to VBScript, perhaps we can be of more assistance. – Doug Deden Mar 12 '19 at 17:30
  • 1
    Windows has two script hosts -- wscript.exe, which uses windows for input and output; and cscript.exe which uses a console window. By default, .vbs files are run with wscript.exe. If you force the script to be run with cscript.exe -- e.g. with a batch file, or even another script -- you'll end up with a visible window which the user can close at any time. – Zev Spitz Mar 12 '19 at 23:44
  • @DougDeden I'm using it for a Citrix published desktop session with restrictions, so sadly I'm not able to use any of the things I mentioned :/ – Geco Mynx Mar 13 '19 at 18:24
  • @ZevSpitz this may not be possible due to the restrictions, but I'll give it a try and then report if it works. Thanks anyways for this tip :) – Geco Mynx Mar 13 '19 at 18:26
  • @ZevSpitz It's working, I've posted an answer for it. Thank you very much. :D – Geco Mynx Mar 14 '19 at 10:38
0

@ZevSpitz Your Tip is working fine, thanks again. :D

This is the way I'm doing it now:

  1. File Structure: 1 Structure
  2. The code of the CaffeineWin.vbs Script: 2 CaffeineWin.vbs
  3. The renamed Script Hosts cscript.exe and wscript.exe in the corresponding Windows Versions: 3 renamed Script Host Versions
  4. The content of the ReadMe.txt. 4 ReadMe.txt Thank you all for your help. :)
|improve this answer|||||
  • Why do you need copies of all the cscript/wscript from every version of Windows? Why not just make a shortcut to the one in the system32 folder? – Zev Spitz Mar 14 '19 at 11:53
  • Because I want to be able to determine the process name of the script. So, if someone else is doing scripting stuff on the servers and for example wants to close all cscript.exe processes, the CaffeineWinC10x64.exe will not be affected by this. – Geco Mynx Mar 14 '19 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.