VB icon

Windows 10 LogOff Shutdown ReBoot PowerOff

Email
Submitted on: 3/28/2019 10:34:48 AM
By: A_X_O 
Level: Beginner
User Rating: Unrated
Compatibility: VbScript (browser/client side)
Views: 1252
 
     A basic and straight-forward Script to offer the choices LogOff Shutdown ReBoot PowerOff to the user and then act on those choices.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Windows 10 LogOff Shutdown ReBoot PowerOff
' Description:A basic and straight-forward Script to offer the choices LogOff Shutdown ReBoot PowerOff to the user and then act on those choices.
' By: A_X_O
'**************************************

'
'-------------------------------------------------------------------------------'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'		Demonstration: Windows 10, LogOff Shutdown ReBoot PowerOff	'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'										'
'	Purpose		: Demonstrates Operating System Management		'
'	-----------------------------------------------------------------------	'
'	Creation Date	: 27/03/2019 [dd/mm/yyyy]				'
'	Version		: 1:1							'
'	Designer	: Fabian						'
'										'
'###############################################################################'
'			MODIFICATION HISTORY					'
'-------------------------------------------------------------------------------'
'										'
'	Version		: 1:0	27/03/2019	Create the Sample		'
'			: 1:1	27/03/2019	Fix minor comment errors 	'
'			: 1:1	27/03/2019	Add Escape options for user	'	
'										'
'-------------------------------------------------------------------------------'		
'
Call SubMain()
'
Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_POWEROFF = 8
'
Public TOKEN
'
Public Const StrQuestionText = "Enter the appropriate number for the task you would like performed."
Public Const StrQuestionErr = "Only numbers from 1 - 4 can be used."
Public Const StrQuestionRetry = "Would you like to try again ?"
Public Const StrQuestionTitle = "Select an Option."
Public Const StrQuestionInTitle = "LogOff Shutdown PowerOff ReBoot ?"
Public Const StrQuestionDefaultVal = "2"
'
Public Const StrLOGOFF = "Enter: 1 to Log-Off" 		'<-- 0 is the Actual Constant
Public Const StrSHUTDOWN = "Enter: 2 to Shutdown"	'<-- 1 is the Actual Constant
Public Const StrREBOOT = "Enter: 3 to Re-Boot"		'<-- 2 is the Actual Constant
Public Const StrPOWEROFF = "Enter: 4 to Power-Off"	'<-- 8 is the Actual Constant
'
Public Const StrConfirm = "Are you sure you want to take this action ?"
Public Const StrConfirmTitle = "Windows Session"
'
Dim objWMIService
Dim colItems
Dim objItem
'
Dim Question
Dim QuestionSelect
Dim RetVal
'
Public Sub SubMain()
On Error Resume Next
'
Set objWMIService = GetObject("winmgmts:{(Shutdown)}\\.\root\CIMV2")
'
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem Where Primary=True") 
'
	QuestionSelect = VbCrLf & VbCrLf & StrLOGOFF & VbCrLf & StrSHUTDOWN & VbCrLf & StrREBOOT & VbCrLf & StrPOWEROFF & VbCrLf 
	Question = InputBox(((StrQuestionText) & (QuestionSelect)),(StrQuestionInTitle),StrQuestionDefaultVal)
'
Rem: Handle the Cancel option in the Input
'
If Question = VbNullString Then: Wscript.Quit
'
Rem: Briefly check to make sure the entered data is what we expect, ie, integers from 1 to 4
'
If Not IsNumeric(Question) Or Question > 4 Or Question < 1 Then
'
	RetVal = MsgBox((StrQuestionErr) & VbCrLf & (StrQuestionRetry),VbSystemModal + VbExclamation + vbRetryCancel,(StrQuestionTitle))
'
Select Case RetVal
'
Case VbRetry
'
Call SubMain()
'
Case VbCancel
'
	WScript.Quit
'
End Select
'
Else
'
Call SystemManagement(Question)
'
End If
'
End Sub
'
Public Sub SystemManagement(ByVal IntAction)
On Error Resume Next
'
Rem:	Added Escape Option
'
Dim ConfirmQuestion
'
	ConfirmQuestion = Msgbox((StrConfirm), VbSystemModal + VbExclamation + VbYesNo, (StrConfirmTitle))
'
Select Case ConfirmQuestion
'
Case VbYes
'
If IntAction = 1 Then
'
	TOKEN = EWX_LOGOFF
'
ElseIf IntAction = 2 Then
'
	TOKEN = EWX_SHUTDOWN
'
ElseIf IntAction = 3 Then
'
	TOKEN = EWX_REBOOT
'
ElseIf IntAction = 4 Then
'
	TOKEN = EWX_POWEROFF
'
End If
'
For Each objItem in colItems 
'
 	objItem.Win32Shutdown TOKEN
'
Next
'
Case VbNo
'
Set objWMIService = Nothing
Set colItems = Nothing
'
	WScript.Quit
'
End Select
'
End Sub
'
'


Other 18 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.