Windows Script (VBS) to always make an application stay open - windows

So an application vital to my business and employees is our Time Clock. It is an exe part of a larger RMS system designed years ago. When you clock in or out, the application automatically closes. I created a Desktop shortcut to make it quicker to open, but I was wondering if there was a way to do this with a Windows Script.
Basic parameters:
Upon startup of the computer, the script is run and the clock.exe application opens
If at any point, clock.exe closes or stops; clock.exe will be reopened by the script.
I messed around with the script myself and managed to have the script open the application, but I tried to use a loop to make it monitor the process which led to many clock applications opening and finally the computer shut down because of overload.
option explicit
DIM strComputer,strProcess
strComputer = "." ' local computer
strProcess = "clock.exe"
' Check if Calculator is running on specified computer (. = local computer)
if isProcessRunning(strComputer,strProcess) then
CreateObject("WScript.Shell").Run("""C:\Users\FrontDesk1\Documents\alwaysClock.vbs""")
else
CreateObject("WScript.Shell").Run("""C:\RMS\pos\clock.exe""")
end if
' Function to check if a process is running
function isProcessRunning(byval strComputer,byval strProcessName)
Dim objWMIService, strWMIQuery
strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
if objWMIService.ExecQuery(strWMIQuery).Count > 0 then
isProcessRunning = true
else
isProcessRunning = false
end if
end function
CreateObject("WScript.Shell").Run("""C:\Users\FrontDesk1\Documents\alwaysClock.vbs""")

Instead of polling to ask if it's running, track events raised when processes stop, watch for clock.exe stopping and restart it. This should use fewer resources ("tell me when we get there" vs. "are we nearly there yet? are we nearly there yet? are we nearly there yet?"), and respond more quickly (without waiting for the next poll to start).
' Monitor process stop trace events.
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessStopTrace = objWMIService.ExecNotificationQuery _
("SELECT * FROM Win32_ProcessStopTrace")
Do
Set objLatestEvent = colProcessStopTrace.NextEvent
Wscript.Echo "Test message: process stopped: " & objLatestEvent.ProcessName
if objLatestEvent.ProcessName = "clock.exe" then
CreateObject("WScript.Shell").Run("""C:\RMS\pos\clock.exe""")
end if
Loop
( Adapted from http://www.vbsedit.com/scripts/misc/events/scr_1218.asp )

object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
So
Set oShell = WScript.CreateObject ("WSCript.shell")
Do
oShell.Run "C:\Folder\Program.exe", 1, true
Loop

You can get inspired by this code from Chancity
A Vbscript to checks the launch of a .bat and .exe programs
Option Explicit
If AppPrevInstance() Then
MsgBox "Instance already running",VbExclamation,"Instance already running"
WScript.Quit
Else
Do
Call Main(Array("c:\toto1.bat","c:\toto2.bat","c:\toto3.bat","%ProgramFiles%\Internet Explorer\iexplore.exe"))
Call Pause(15) 'Pause de 15 minutes
Loop
End If
'**************************************************************************
Sub Main(colProcessPaths)
Dim ProcessPath
For Each ProcessPath In colProcessPaths
CheckProcess(ProcessPath)
Next
End Sub
'**************************************************************************
Sub CheckProcess(ProcessPath)
Dim ProcessName : ProcessName = StripProcPath(ProcessPath)
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " & CommandLineLike(ProcessName))
If .Count = 0 Then
With CreateObject("WScript.Shell")
.Run DblQuote(ProcessPath)
End With
Else
Exit Sub
End if
End With
End With
End Sub
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'**************************************************************************
Sub Pause(Minutes)
Wscript.Sleep(Minutes*1000*60)
End Sub
'**************************************************************************
Function StripProcPath(ProcessPath)
Dim arrStr : arrStr = Split(ProcessPath, "\")
StripProcPath = arrStr(UBound(arrStr))
End Function
'**************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'**************************************************************************
'Fonction pour ajouter les doubles quotes dans une variable
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************

Related

Can someone help me understand this code ? Infected with Trojan Dropper VBS script

I wanna say sorry first ,
This isnt probly the best place to ask things like these , but i was infected by a virus and it seems to left traces.
If someone can understand the code behind some scripts i would be very grateful.
Option Explicit
Dim ProcessPath,WshShell
ProcessPath = "%Windir%\System32\Notepad.exe"
Set WshShell = CreateObject("WScript.Shell")
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF &_
CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"
WScript.Quit
Else
Do
Pause(10) ' Pause 10 seconds
If CheckProcess(DblQuote(ProcessPath)) = False Then
Call Logoff()
End If
Loop
End If
'**************************************************************************
Function CheckProcess(ProcessPath)
Dim strComputer,objWMIService,colProcesses,Tab,ProcessName
strComputer = "."
Tab = Split(ProcessPath,"\")
ProcessName = Tab(UBound(Tab))
ProcessName = Replace(ProcessName,Chr(34),"")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '"& ProcessName & "'")
If colProcesses.Count = 0 Then
CheckProcess = False
Else
CheckProcess = True
End if
End Function
'**************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************
Sub Logoff()
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Exec("C:\Users\Sblck\AppData\Local\AppVShNotifyt.exe")
Set objShell = Nothing
Wscript.Quit
End sub
'**************************************************************************
Sub Pause(Secs)
Wscript.Sleep(Secs * 1000)
End Sub
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'***************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'****************************************************************************
I hope i formated the code right , there is some other script but in a txt form , the diference is in objShell.Exec it remotes to "%working%" , like this:
objShell.Exec("%working%")^
I'm gonna post pastebin's of it if it makes it easier
AppVShNotifytvbs.vbs PasteBin link
AppVShNotifytvbs.txt Pastebin link
First, it check previous instance to see that the current script is already running using another process ID.
Then, every 10 seconds is checks if notepad is running. If it's not, it calls AppVShNotifyt.exe

How to close a specific folder with VBScript?

I am trying to make a simple program with VBScript that closes a specific folder every time it gets opened, thus denying access to that folder. I've successfully used this code right here for many folders, but for some reason it doesn't work with C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp.
Do
WindowTitle = "FOLDERNAME"
Set shell = CreateObject("WScript.Shell")
success = shell.AppActivate(WindowTitle)
If success Then shell.SendKeys "%{F4}"
Loop
Is there any way that I can deny access to that specific folder using .vbs files?
You can try like this :
myfolder = "C:\temp"
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
If w.document.folder.self.Path = myfolder Then w.Quit
Next
And here is a complete example to close your temporary folder :
Option Explicit
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF &_
CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"
WScript.Quit
Else
Dim MyFolder,ws
Set ws = CreateObject("wscript.shell")
Myfolder = ws.ExpandEnvironmentStrings("%temp%")
Do
Call CloseThis(MyFolder)
wscript.sleep 1000
Loop
End If
'*********************************************************************************************
Sub CloseThis(Folder)
Dim sh,w
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
If w.document.folder.self.Path = Folder Then w.Quit
Next
End Sub
'*********************************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'*********************************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'*********************************************************************************************

check if process is running VBS, when closed log off

Apologies i am a bit of a newbie at vbs and was wondering if i could get some help.
I want to write some vbscript to check if a process name is still running, if it is keep checking the process. If the process-name is not running log off windows.
This is what i have tried so far
set service = GetObject ("winmgmts:")
for each Process in Service.InstancesOf ("Win32_Process")
If Process.Name = "notepad.exe" False ,then
Set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.run "shutdown.exe -L -F"
'wscript.echo "Notepad running"
'wscript.quit
End If
next
please let me know if you need any more information :)
This vbscript can did the trick :
Option Explicit
Dim ProcessPath,WshShell
ProcessPath = "%Windir%\System32\Notepad.exe"
Set WshShell = CreateObject("WScript.Shell")
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF &_
CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"
WScript.Quit
Else
Do
Pause(10) ' Pause 10 seconds
If CheckProcess(DblQuote(ProcessPath)) = False Then
Call Logoff()
End If
Loop
End If
'**************************************************************************
Function CheckProcess(ProcessPath)
Dim strComputer,objWMIService,colProcesses,Tab,ProcessName
strComputer = "."
Tab = Split(ProcessPath,"\")
ProcessName = Tab(UBound(Tab))
ProcessName = Replace(ProcessName,Chr(34),"")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '"& ProcessName & "'")
If colProcesses.Count = 0 Then
CheckProcess = False
Else
CheckProcess = True
End if
End Function
'**************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************
Sub Logoff()
Dim ws,Command,Execution
Set ws = CreateObject("wscript.Shell")
Command = "Cmd /c shutdown.exe -L -F"
Execution = ws.run(Command,0,True)
End sub
'**************************************************************************
Sub Pause(Secs)
Wscript.Sleep(Secs * 1000)
End Sub
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'***************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'****************************************************************************

vbscript how can you see if a program is open then close it

How can I use vbscript to determine if a program is running or open and, if it is, close a different program?
For example, how could I check to see if game.exe is running and, if it is, then close google.exe?
Here is an example that show you how to check a process by name and if the check is positive so it kill it.
For example i choose the Game as Calc.exe and if the script found it, it will be killed, and also for internet explorer and Google Chrome
Option Explicit
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"
WScript.Quit
Else
Dim Game,Browser1,Browser2
Game = "%windir%\system32\calc.exe"
Browser1 = "%ProgramFiles%\Google\Chrome\Application\chrome.exe"
Browser2 = "%ProgramFiles%\Internet Explorer\iexplore.exe"
Do
Call Main(Array(Game,Browser1,Browser2))
Call Pause(1) 'Sleeping for 1 minute
Loop
End If
'**************************************************************************
Sub Main(colProcessPaths)
Dim ProcessPath
For Each ProcessPath In colProcessPaths
CheckProcess(ProcessPath)
Next
End Sub
'**************************************************************************
Sub CheckProcess(ProcessPath)
On error resume Next
Dim Process,objWMIService,colProcesses,wshShell,btn,Timeout,User
Dim ProcessName : ProcessName = StripProcPath(ProcessPath)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE Commandline LIKE " & CommandLineLike(ProcessName))
For Each Process in colProcesses
If colProcesses.Count > 0 Then
Set wshShell = CreateObject("WScript.Shell")
User = CreateObject("WScript.Network").UserName
Timeout = 30 'Call the Popup method with a 30 seconds timeout.
btn = WshShell.Popup("Hello "& DblQuote(User) & " !" & vbcr &_
"Your Administrator has requested you to log out of this application after work. " & vbcr &_
"We have detected you are still using the program : "& DblQuote(ProcessName) & vbcr &_
"Please press on cancel button if you are still at your machine ?",Timeout,"Question", vbOKCancel + vbQuestion)
Select Case btn
' Yes button pressed.
case 1
Process.Terminate(0)
' No button pressed.
case 2
Exit Sub
' Timed out.
case -1
Process.Terminate(0)
End Select
Else
Exit Sub
End if
Next
End Sub
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'**************************************************************************
Sub Pause(Minutes)
Wscript.Sleep(Minutes*1000*60)
End Sub
'**************************************************************************
Function StripProcPath(ProcessPath)
Dim arrStr : arrStr = Split(ProcessPath, "\")
StripProcPath = arrStr(UBound(arrStr))
End Function
'**************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'**************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************

Multiple instances of MSE7 open when I boot my computer

I am running various VBS scripts on our clients using Group Policy. On most clients this works fine. But on my computer (the one I created the scripts on), I always have multiple instances of MSE7 running after booting my computer - one for each of the VBS scripts I have deployed via Group Policy.
Can anyone help me understand why this is happening and / or how to stop it!
Thanks.
Andrew
With this script you can find any process by its commandline and choose to kill them. Just give a try !
Option Explicit
Dim Titre,Copyright,fso,ws,NomFichierLog,temp,PathNomFichierLog,OutPut,Count
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"
WScript.Quit
Else
Copyright = "[© Hackoo © 2015 ]"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject( "Wscript.Shell" )
NomFichierLog="Killed_Process.txt"
temp = ws.ExpandEnvironmentStrings("%temp%")
PathNomFichierLog = temp & "\" & NomFichierLog
Set OutPut = fso.CreateTextFile(temp & "\" & NomFichierLog,1)
Call Find("wscript.exe")
Call Explorer(PathNomFichierLog)
End If
'***************************************************************************************************
Function Explorer(File)
Dim ws
Set ws = CreateObject("wscript.shell")
ws.run "Explorer "& File & "\",1,True
end Function
'***************************************************************************************************
Sub Find(MyProcess)
Dim colItems,objItem,Processus,Question
Titre = " Process "& DblQuote(MyProcess) &" running "
Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
& "Where Name like '%"& MyProcess &"%' AND NOT commandline like " & CommandLineLike(WScript.ScriptFullName) & "",,48)
Count = 0
For Each objItem in colItems
Count= Count + 1
Processus = objItem.CommandLine
Question = MsgBox ("Would do you like to kill this script : " & DblQuote(Processus) & " ?" ,VBYesNO+VbQuestion,Titre+Copyright)
If Question = VbYes then
objItem.Terminate(0)'To kill the process
OutPut.WriteLine Processus
else
Count= Count - 1 'decrementing the count of -1
End if
Next
OutPut.WriteLine String(100,"*")
OutPut.WriteLine count & Titre & "were killed"
OutPut.WriteLine String(100,"*") & VbCrLF
End Sub
'**************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'**************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'**************************************************************************

Resources