All Forums >> [Scripting] >> WSH & Client Side VBScript >> Log on script to log off user after log on for 30 mins Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Hi, i´ve cut out the following parts from a bigger script i once made, so i havnt tried the code myself, but it should atleast point you in the right direction for getting the "logout" part of ur script. You can prolly clean it up a lil as well. Add a timer in the beginning of the script and/or make some popups waring about "5 mins left" and you should have a good start.
Hope it helps!
/CM
_____________________________
-There is only 10 sorts of people, those who understand binary and those who dont.
Thank you very much for your help. I'm still very new with VBScript, so this is kinda over head for me. I want to help out a friend. Would you please help me with a full script? or if you are busy, please point me to a good place where I can start learning VBScript as quick as I can. Thank you. I really appreciate your kindness.
' ' *********************************************************************** ' * ' * LogoffIn30 - Logs off the current user in 30 minutes ' * John Holliday - 12/29/2005 - 1.0 ' * ChangeAuthorName - DateOfChange - IteratedVersionNumber ' * ' *********************************************************************** ' Option Explicit
Public gobjWshShell
Sub ScriptInit() Set gobjWshShell = CreateObject("WScript.Shell") End Sub
Function Sleep(lintNumOfSeconds, lblnUseMilliseconds) ' Put False as the second argument to use standard seconds. ' Put True to use milliseconds. If lblnUseMilliseconds = False Then lintNumOfSeconds = lintNumOfSeconds * 1000 End If WScript.Sleep lintNumOfSeconds End Function
Sub ShutDown(lintShutdownType) Dim strComputer Dim OpSys Dim OpSysSet strComputer = "." If lintShutDownType = "" then lintShutDownType = 4 End If Set OpSysSet = GetObject("winmgmts:{(Debug,Shutdown)}//" & strComputer & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true") For Each OpSys in OpSysSet OpSys.win32Shutdown(lintShutdownType) Next ' The numbers in the Decimal column are for the lintShutDownType variable. ' Action Decimal Binary ' Logoff 0 0000 ' Reboot 2 0010 ' Force Logoff 4 0100 ' Force Reboot 6 0110 ' Powerdown 8 1000 ' Force Powerdown 12 1100 End Sub
Sub RunFor30() On Error Resume Next Dim lblnIsDone Dim lintNumMinutes lintNumMinutes = 0 Do lblnIsDone = False Sleep 60, False lintNumMinutes = lintNumMinutes + 1 Select Case lintNumMinutes Case 25 gobjWshShell.Popup "Warning!" & vbCrLf & vbCrLf & "This workstaion will be logged off in 5 minutes!" & vbCrLf & "Please save your data and close any open applications." & vbCrLf & "This box will close in 15 seconds.", 15, "Logoff Warning", vbExclamation + vbOKOnly + vbSystemModal Case 30 lblnIsDone = True ShutDown 4 Case Else ' Do nothing End Select Loop Until lblnIsDone = True End Sub
' Processing starts here
ScriptInit RunFor30 WScript.Quit
' ***************
That's the end of the script. Remember that the "gobjWshShell.Popup" line is all on ONE line, not two. Let me know if it works.
I hope you won't mind being called 'a fast gun'! I do like your script/style. Using "Public" instead of simple "Dim" to mark global variables, stating variable types explicitly, splitting the script in reusable, independent parts, explaining the used numeric constants - I'm very impressed.
So don't take my following questions as criticism - I just want to lure you into a discussion about "VBScript - Best Practices".
Why is gobjWshShell global - it's used in RunFor() only?
Why do you use "lblnUseMilliseconds = False" or "lblnIsDone = True"?
Woudn't RunFor() be more usefull if given the MinutesToRun as a parameter?
Why did you code the action to do after the N minutes into RunFor()?
Dont forget to use the "code" tags to enbedd your code a separate window within the post (its the "<%" symbol next to the font size when you are creating a new post)
Regards Matt
_____________________________
-There is only 10 sorts of people, those who understand binary and those who dont.