Solved

VB script choices

Posted on 2009-07-09
16
847 Views
Last Modified: 2012-08-13
I have two scripts I need to merge.

I have a script that pops up an informational window and waits X number of seconds, and then another that auto pops up and waits Y number of seconds, and then goes away and then a shutdown command is issued via Altiris.  

The second script wants to approach this a bit differently, by asking if the PC should be auto turned off, or not.  

How can I use the second script but have it essentially delay forever if they choose not to shut down, and to close/finish in 10 minutes if they DO choose to shutdown?
First Script:
 

dim naptime, naptime2, objDOS

naptime = 30

naptime2 = 20

set objDOS = CreateObject ("WScript.Shell")

objDOS.Popup "CONSERVATION IS IMPORTANT!" & vbcrlf & "" & vbcrlf & "IN AN EFFORT TO SAVE ENERGY WE ARE SHUTTING DOWN YOUR PC FOR THE NIGHT." & vbcrlf & "" & vbcrlf & " *** YOU HAVE 10 MINUTES BEFORE SHUT DOWN. PLEASE SAVE YOUR WORK NOW! ***" & vbcrlf & "" & vbcrlf & "IF YOU NEED TO USE YOUR PC AFTER IT HAS SHUT DOWN, YOU CAN POWER IT BACK ON AND CONTINUE WORKING." & vbcrlf & "" & vbcrlf & "IF YOUR PC IS NOT ON IN THE MORNING, POWER IT ON MANUALLY THEN." & vbcrlf & "" & vbcrlf & "(ATTENTION: Clicking OK now will shutdown your PC in FIVE minutes!)", naptime , "Information Technology Services is GOING GREEN", 0

objDOS.Popup "YOU HAVE 5 MINUTES BEFORE SHUTDOWN!" & vbcrlf & "" & vbcrlf & "ANY UNSAVED WORK MAY BE LOST!" & vbcrlf & "" & vbcrlf & "(Clicking OK now will shutdown your PC instantly!)" & vbcrlf & "" & vbcrlf & "PLEASE TURN YOUR MONITOR OFF NOW! HELP US CONSERVE ENERGY!", naptime2 , "***PC SHUTDOWN IN 5 MINUTES!***", 0
 

---------------------------------
 

Second Script:
 

Option Explicit
 

MsgBox "CONSERVATION IS IMPORTANT!" & vbcrlf & "" & vbcrlf & "IN AN EFFORT TO SAVE ENERGY WE WOULD LIKE TO SHUT DOWN YOUR PC FOR THE NIGHT." & vbcrlf & "" & vbcrlf & "" & vbcrlf & "                                  *** IF YOU CHOOSE TO SHUT DOWN," & vbcrlf & "              YOU HAVE 10 MINUTES TO SAVE YOUR FILES AND CLOSE PROGRAMS. ***" & vbcrlf & "" & vbcrlf & "" & vbcrlf & "IF YOU NEED CHOOSE NOT TO SHUT DOWN NOW, PLEASE POWER YOUR PC OFF WHEN FINISHED." & vbcrlf & "" & vbcrlf & "IF YOUR PC IS NOT ON IN THE MORNING, POWER IT ON MANUALLY." & vbcrlf & "" & vbcrlf & "(CLICK OK NOW TO CHOOSE TO SHUTDOWN OR CANCEL THIS PROGRAM.)", vbInformation, "Information Technology Services is GOING GREEN"
 

If MsgBox("Click YES to shutdown in 10 minutes, click NO to turn computer off manually.", vbQuestion + vbYesNo, "SHUTDOWN?") = vbYes Then

	MsgBox "Computer will shut down in 10 minutes!", vbInformation

Else

	MsgBox "Computer will NOT auto shut down." & vbcrlf & "Please turn off your PC when finished.", vbInformation

End If

Open in new window

0
Comment
Question by:ITS-DCO
  • 9
  • 7
16 Comments
 
LVL 2

Expert Comment

by:whomJesusloves
Comment Utility
Here's a related post:

http://www.experts-exchange.com/Microsoft/Applications/Q_24542687.html



Option Explicit

 

MsgBox "CONSERVATION IS IMPORTANT!" & vbcrlf & "" & vbcrlf & "IN AN 
 

EFFORT TO SAVE ENERGY WE WOULD LIKE TO SHUT DOWN YOUR PC FOR THE 
 

NIGHT." & vbcrlf & "" & vbcrlf & "" & vbcrlf & "                    
 

              *** IF YOU CHOOSE TO SHUT DOWN," & vbcrlf & "         
 

     YOU HAVE 10 MINUTES TO SAVE YOUR FILES AND CLOSE PROGRAMS. 
 

***" & vbcrlf & "" & vbcrlf & "" & vbcrlf & "IF YOU NEED CHOOSE NOT 
 

TO SHUT DOWN NOW, PLEASE POWER YOUR PC OFF WHEN FINISHED." & vbcrlf 
 

& "" & vbcrlf & "IF YOUR PC IS NOT ON IN THE MORNING, POWER IT ON 
 

MANUALLY." & vbcrlf & "" & vbcrlf & "(CLICK OK NOW TO CHOOSE TO 
 

SHUTDOWN OR CANCEL THIS PROGRAM.)", vbInformation, "Information 
 

Technology Services is GOING GREEN"

 

If MsgBox("Click YES to shutdown in 10 minutes, click NO to turn 
 

computer off manually.", vbQuestion + vbYesNo, "SHUTDOWN?") = vbYes 
 

Then

        MsgBox "Computer will shut down in 10 minutes!", 
 

vbInformation

	start_timer()

Else

        MsgBox "Computer will NOT auto shut down." & vbcrlf & 
 

"Please turn off your PC when finished.", vbInformation

End If
 

Function start_timer()

	Set objShell = CreateObject("Wscript.Shell")

	objShell.Run "shutdown -s -t 600 -f"

End Funciton

Open in new window

0
 
LVL 2

Expert Comment

by:whomJesusloves
Comment Utility
Sorry - it goofed up the text when pasting from Notepad
0
 

Author Comment

by:ITS-DCO
Comment Utility
I will test! Thanks!
0
 

Author Comment

by:ITS-DCO
Comment Utility
That sort of helps, but I was hoping for something I could customize to continue to remind someone to shutdown.

I've tried modifying the script in the link to call a file that would wait a set period and then run the entire VB script again, but it shows the initial msgbox and then does nothing after that.  Any thoughts?
0
 

Author Comment

by:ITS-DCO
Comment Utility
Attached is my current code... I'm also wondering if I can hide the delay message that shutdown.exe creates.
Dim objShell, strComputer, strInput

Dim strShutdown

 

' This parameter is for when the user clicks on the yes box.(adjust -t as needed - default 5 min.)

strShutdown1 = "\\dss\apps\shutdown\shutdown.exe -u -t 300 -f >nul" & strComputer

' This parameter is for when the user clicks on the no box. 

' strShutdown2 = "cscript \\dss\apps\shutdown\msg.vbs //NoLogo >nul" & strComputer

' This parameter is for when the user does nothing. (adjust -t as needed - default 0 min.)

' If this is not needed, REM out the line with "        objShell.Run strShutdown3"

strShutdown3 = "\\dss\apps\shutdown\shutdown.exe -u -t 0 -f" & strComputer

 

Const wshYes = 6

Const wshNo = 7

Const wshYesNoDialog = 4

Const wshQuestionMark = 32

 

Set objShell = CreateObject("Wscript.Shell")

 

intReturn = objShell.Popup("CONSERVATION IS IMPORTANT!" & vbcrlf & "" & vbcrlf & "IN AN EFFORT TO SAVE ENERGY WE WANT TO SHUT DOWN YOUR PC FOR THE NIGHT." & vbcrlf & "" & vbcrlf & " *** CLICK YES AND YOUR COMPUTER WILL SHUT DOWN IN 5 MINUTES. PLEASE SAVE YOUR WORK NOW! ***" & vbcrlf & "" & vbcrlf & "CLICK NO TO CANCEL. DO NOTHING AND YOUR PC WILL SHUT DOWN AUTOMATICALLY." & vbcrlf & "" & vbcrlf & "IF YOUR PC IS NOT ON IN THE MORNING, POWER IT ON MANUALLY.", vbInformation, "Information Technology Services is GOING GREEN", wshYesNoDialog + wshQuestionMark)

 

If intReturn = wshYes Then

        Wscript.Echo "You clicked the Yes button.  Your computer will shutdown in 5 minutes. Please close all applications."

        objShell.Run strShutdown1

ElseIf intReturn = wshNo Then

        Wscript.Echo "CONSERVATION IS IMPORTANT!  WHEN YOU ARE FINISHED USING YOUR PC FOR THE NIGHT PLEASE TURN IT AND THE MONITOR OFF."

Else

        objShell.Run strShutdown3

End If

Open in new window

0
 
LVL 2

Expert Comment

by:whomJesusloves
Comment Utility
i wrote this in answer to the post before your latest:


If no is clicked, it will wait 10 mins, and run the whole script again.
If yes is clicked, it will shutdown in 10 mins.

To avoid the shutdown prompt, you can use:
                Wscript.Sleep(600*1000)
                objShell.Run "shutdown -s -t 0 -f"


instead of :
            Set objShell = CreateObject("WScript.Shell")
            objShell.Run "shutdown -s -t 600 -f"
Option Explicit

Dim objShell, mins

 

main()

 

Function main()

 	MsgBox "CONSERVATION IS IMPORTANT!" & vbcrlf & "" & vbcrlf &_

		"IN AN EFFORT TO SAVE ENERGY WE WOULD LIKE TO SHUT DOWN YOUR PC FOR THE NIGHT." &_

		vbcrlf & "" & vbcrlf & "" & vbcrlf & "                                  *** IF YOU CHOOSE TO SHUT DOWN," &_

		vbcrlf & "              YOU HAVE 10 MINUTES TO SAVE YOUR FILES AND CLOSE PROGRAMS. ***" &_

		vbcrlf & "" & vbcrlf & "" & vbcrlf &_

		"IF YOU NEED CHOOSE NOT TO SHUT DOWN NOW, PLEASE POWER YOUR PC OFF WHEN FINISHED." & vbcrlf &_

		"" & vbcrlf & "IF YOUR PC IS NOT ON IN THE MORNING, POWER IT ON MANUALLY." & vbcrlf & "" & vbcrlf &_

		"(CLICK OK NOW TO CHOOSE TO SHUTDOWN OR CANCEL THIS PROGRAM.)",_

		vbInformation, "Information Technology Services is GOING GREEN"

	 
 

	If MsgBox("Click YES to shutdown in 10 minutes, click NO to turn computer off manually.",_

			vbQuestion + vbYesNo,"SHUTDOWN?") = vbYes Then

		MsgBox "Computer will shut down in 10 minutes!", vbInformation

		

		Set objShell = CreateObject("WScript.Shell")

		objShell.Run "shutdown -s -t 600 -f"

	Else

		MsgBox "Computer will NOT auto shut down." & vbcrlf &_

			"Please turn off your PC when finished.", vbInformation

		WScript.Sleep(6*1000) ''That's 600 seconds * 1000(it's measured in milliseconds).

		main()  ''This reruns the main() function.

	End If

End Function

Open in new window

0
 

Author Comment

by:ITS-DCO
Comment Utility
Thanks! I will try it!
0
 
LVL 2

Expert Comment

by:whomJesusloves
Comment Utility
correction on my previous post:
keep the 'CreateObject' line in there, or it will throw an error.

     Set objShell = CreateObject("WScript.Shell")
     Wscript.Sleep(600*1000)
     objShell.Run "shutdown -s -t 0 -f"
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ITS-DCO
Comment Utility
That seems to work.  What if no one responds at ALL to the first pop-up?
0
 

Author Comment

by:ITS-DCO
Comment Utility
By this, I mean, can I have it shutdown the PC if no one touches the initial popup in x minutes?
0
 
LVL 2

Expert Comment

by:whomJesusloves
Comment Utility
It'll sit there until they do.  If you wanted, you could start a 30 min shutdown (showing the countdown screen along with the popup)  and cancel it when/if they respond to the popup.
0
 

Author Comment

by:ITS-DCO
Comment Utility
I'd rather not show any countdown. Just have the vbs autoexecute a shutdown if not touched.  Is that possible?
0
 
LVL 2

Accepted Solution

by:
whomJesusloves earned 500 total points
Comment Utility
Using the Wscript.popup method, you can time a messagebox.

See if this works for you.
Option Explicit

Dim objShell, mins, strMsg, intReturn

 

main()

 

Function main()

	Const wshYes = 6

	Const wshNo = 7

	Const vbOKOnly = 0

	Const wshYesNoDialog = 4

	Const wshQuestionMark = 32 

	

	Set objShell = CreateObject("Wscript.Shell")
 

	strMsg = "CONSERVATION IS IMPORTANT!" & vbcrlf & "" & vbcrlf &_

			"IN AN EFFORT TO SAVE ENERGY WE WOULD LIKE TO SHUT DOWN YOUR PC FOR THE NIGHT." &_

			vbcrlf & "" & vbcrlf & "" & vbcrlf & "                                  *** IF YOU CHOOSE TO SHUT DOWN," &_

			vbcrlf & "              YOU HAVE 10 MINUTES TO SAVE YOUR FILES AND CLOSE PROGRAMS. ***" &_

			vbcrlf & "" & vbcrlf & "" & vbcrlf &_

			"IF YOU NEED CHOOSE NOT TO SHUT DOWN NOW, PLEASE POWER YOUR PC OFF WHEN FINISHED." & vbcrlf &_

			"" & vbcrlf & "IF YOUR PC IS NOT ON IN THE MORNING, POWER IT ON MANUALLY." & vbcrlf & "" & vbcrlf &_

			"(CLICK OK NOW TO CHOOSE TO SHUTDOWN OR CANCEL THIS PROGRAM.)"

	

	intReturn = objShell.Popup(strMsg, 20, "Information Technology Services is GOING GREEN", vbOKOnly)

	

	If intReturn = -1 then

		shutdown()

	Else

		intReturn = objShell.Popup("Click YES to shutdown in 10 minutes, click NO to turn computer off manually.", 30, "SHUTDOWN?", wshQuestionMark + vbYesNo)
 

		If intReturn <> wshNo Then

			shutdown()

		Else

				MsgBox "Computer will NOT auto shut down." & vbcrlf &_

						"Please turn off your PC when finished."

				WScript.Sleep(600*1000) ''That's 600 seconds * 1000(it's measured in milliseconds).

				main()  ''This reruns the main() function.

		End If

	End If

End Function
 
 

Function Shutdown()

	intReturn = objShell.Popup("Computer will shut down in 10 minutes!", 20, "Information Technology Services is GOING GREEN", vbOKOnly)

	Set objShell = CreateObject("WScript.Shell")

	Wscript.Sleep(600*1000)

	objShell.Run "shutdown -s -t 0 -f"

End Function

Open in new window

0
 
LVL 2

Expert Comment

by:whomJesusloves
Comment Utility
0
 

Author Comment

by:ITS-DCO
Comment Utility
Thanks for your help!  This is working well (except that when you say NO the first time, it waits 10 mins and asks again, which is great, and no matter what you say then reboots you in 10 mins).  I will play with the settings.

Thanks!
0
 

Author Comment

by:ITS-DCO
Comment Utility
Correction. It's working perfectly.  I love it!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now