Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for logoff.vbs

(view source code of logoff.vbs as plain text)

  1. ' Logoff.vbs, Version 1.00
  2. ' Logoff current user on any WMI enabled computer on the network
  3. '
  4. ' Adapted from posts by Alex Angelopoulos on www.developersdex.com
  5. ' and Michael Harris on microsoft.public.scripting.vbscript
  6. '
  7. ' Written by Rob van der Woude
  8. ' http://www.robvanderwoude.com
  9.  
  10. ' Check command line parameters
  11. Select Case WScript.Arguments.Count
  12. 	Case 0
  13. 		' Default is local computer if none specified
  14. 		strComputer = "."
  15. 	Case 1
  16. 		Select Case WScript.Arguments(0)
  17. 			' "?", "-?" or "/?" invoke online help
  18. 			Case "?"
  19. 				Syntax
  20. 			Case "-?"
  21. 				Syntax
  22. 			Case "/?"
  23. 				Syntax
  24. 			Case Else
  25. 				strComputer = WScript.Arguments(0)
  26. 		End Select
  27. 	Case Else
  28. 		' More than 1 argument is not allowed
  29. 		Syntax
  30. End Select
  31.  
  32. ' Define some constants that can be used in this script;
  33. ' logoff = 0 (no forced close of applications) or 5 (forced);
  34. ' 5 works OK in Windows 2000, but may result in power off in XP
  35. Const EWX_LOGOFF   = 0
  36. Const EWX_SHUTDOWN = 1
  37. Const EWX_REBOOT   = 2
  38. Const EWX_FORCE    = 4
  39. Const EWX_POWEROFF = 8
  40.  
  41. ' Connect to computer
  42. Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//" & strComputer & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
  43.  
  44. ' Actual logoff
  45. for each OpSys in OpSysSet
  46. 	OpSys.Win32Shutdown EWX_LOGOFF
  47. next
  48.  
  49. ' Done
  50. WScript.Quit(0)
  51.  
  52.  
  53. Sub Syntax
  54. msg = vbCrLf & "Logoff.vbs,  Version 1.00" & vbCrLf & _
  55.       "Logoff the current user of any WMI enabled computer on the network." & _
  56.       vbCrLf & vbCrLf & "Usage:  CSCRIPT  LOGOFF.VBS  [ computer_name ]" & _
  57.       vbCrLf & vbCrLf & _
  58.       "Where:  " & Chr(34) & "computer_name" & Chr(34) & _
  59.       "  is the name of the computer to be logged off" & vbCrLf & _
  60.       "                         (without leading backslashes); default is " & _
  61.       Chr(34) & "." & Chr(34) & vbCrLf & _
  62.       "                         (the local computer)." & vbCrLf & vbCrLf & _
  63.       "Written by Rob van der Woude" & vbCrLf & _
  64.       "http://www.robvanderwoude.com" & vbCrLf & vbCrLf & _
  65.       "Based on posts by Alex Angelopoulos on www.developersdex.com" & _
  66.       vbCrLf & _
  67.       "and Michael Harris on microsoft.public.scripting.vbscript" & vbCrLf
  68. Wscript.Echo(msg)
  69. Wscript.Quit(1)
  70. End Sub
  71.  

page last uploaded: 2017-04-06, 13:33