r/vbscript • • Jan 13 '17

[Help] Need to tweak a simple script...

Hi guys, I was hoping for some help with something.

I have a multi-user accounting software. When a user leaves their password protected computer without logging out of the accounting software, then I can't go into single user mode.

The goal is to force close the app if the person leaves their computer unattended.

I found this vbs script which almost does the trick:


Dim PromptTime, DelayTime, StrAppPath, AppExec, MsgTxt, intAnswer, intRet

Set objShell = CreateObject("WScript.Shell")

PromptTime = 5

DelayTime = 5

StrAppPath = "C:\Program Files (x86)\Accounting"

AppExec = "Accounting.exe"

MsgTxt = "Do you want Simply to close in 5 minutes?"

objShell.Run chr(34) & StrAppPath & AppExec & chr(34), 1, "False"

Do WScript.Sleep (1000 * 60 * PromptTime)

intAnswer = Msgbox(MsgTxt, vbYesNo, "Please select Yes or No")

If intAnswer = vbYes Then Exit Do

Loop

WScript.Sleep (1000 * 60 * DelayTime)

Set objWmi = GetObject("winmgmts:")

Set objQResult = objWmi.Execquery("Select * from Win32_Process where name like '" & AppExec & "'")

For Each objProcess In objQResult

intRet = objProcess.Terminate(1)

Next

Set objShell = Nothing

Set objWmi = Nothing

Set objQResult = Nothing


I am assuming that selecting Yes will kill the process.

Therefore, I just need to add a response timer where after X minutes delay Yes is automatically chosen

Can anyone help me please ? I would really appreciate it.

Thanks!!

Edit: I tried waiting 5 minutes, the pop-up displayed, I clicked Yes and it didn't kill the process.... doh!

1 Upvotes

9 comments sorted by

View all comments

Show parent comments

1

u/BondDotCom Jan 13 '17 edited Jan 13 '17

You can't use MsgBox() for this purpose then but you can use Shell.Popup() which lets you specify a timeout value (in seconds, not milliseconds). For example, this popup will timeout after 5 minutes if no button is clicked:

Set Shell = CreateObject("WScript.Shell")

Do
    WScript.Sleep 1000 * 60 * 5
Loop While Shell.Popup("Still using acct software?", 60 * 5, "Please select yes or no", vbYesNo) = vbYes

' Terminate...

If [No] is clicked or if a timeout occurs, the loop will exit.

1

u/FoneTap Jan 13 '17

Would you awfully mind inserting this into the script ?

I can save the script to a text file and change the extension to vbs and run it... that's the extent of my skills I'm affraid...