Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for bootstate.vbs

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

  1. Option Explicit
  2.  
  3. Dim arrSubKeys
  4. Dim colItems, objItem, objReg, objWMIService
  5. Dim strSubKey
  6.  
  7. Const HKEY_LOCAL_MACHINE = &H80000002
  8.  
  9. If WScript.Arguments.Count > 0 Then Syntax
  10.  
  11. On Error Resume Next
  12.  
  13. Set objReg = GetObject( "winmgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv" )
  14. If objReg.EnumKey( HKEY_LOCAL_MACHINE, "SYSTEM\ControlSet001\Control", arrSubKeys ) = 0 Then
  15. 	If Not Err And Not IsNull( arrSubKeys ) And Not IsEmpty( arrSubKeys ) Then
  16. 		For Each strSubKey In arrSubKeys
  17. 			If UCase( strSubKey ) = "MININT" Then
  18. 				WScript.Echo "Windows PE"
  19. 				Abort 3
  20. 			End If
  21. 		Next
  22. 	End If
  23. Else
  24. 	Syntax
  25. End If
  26.  
  27. Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
  28. Set colItems      = objWMIService.ExecQuery( "SELECT * FROM Win32_ComputerSystem" )
  29. If Not Err And Not IsNull( colItems ) And Not IsEmpty( colItems ) Then
  30. 	For Each objItem in colItems
  31. 		Select Case objItem.BootupState
  32. 			Case "Normal boot":
  33. 				WScript.Echo "Normal"
  34. 				Abort 0
  35. 		    Case "Fail-safe boot":
  36. 		    	WScript.Echo "Safe mode"
  37. 		    	Abort 1
  38. 		    Case "Fail-safe with network boot" :
  39. 		    	WScript.Echo "Safe mode with network"
  40. 		    	Abort 2
  41. 		End Select
  42. 	Next
  43. End If
  44.  
  45. On Error Goto 0
  46.  
  47. WScript.Echo "Unknown"
  48. Abort -1
  49.  
  50.  
  51. Sub Abort( intRC )
  52. 	On Error Resume Next
  53. 	Set objReg        = Nothing
  54. 	Set colItems      = Nothing
  55. 	Set objWMIService = Nothing
  56. 	On Error Goto 0
  57. 	WScript.Quit intRC
  58. End Sub
  59.  
  60.  
  61. Sub Syntax( )
  62. 	Dim strMsg
  63. 	strMsg = "BootState.vbs,  Version 1.00" _
  64. 	       & vbCrLf _
  65. 	       & "Show Windows' boot state" _
  66. 	       & vbCrLf & vbCrLf _
  67. 	       & "Usage:    [ CSCRIPT.EXE //NoLogo ]  BootState.vbs" _
  68. 	       & vbCrLf & vbCrLf _
  69. 	       & "Notes:    Boot state is returned as string and as return code (""errorlevel""):" _
  70. 	       & vbCrLf _
  71. 	       & "              ""Normal""                    (return code = 0)" _
  72. 	       & vbCrLf _
  73. 	       & "              ""Safe mode""                 (return code = 1)" _
  74. 	       & vbCrLf _
  75. 	       & "              ""Safe mode with network""    (return code = 2)" _
  76. 	       & vbCrLf _
  77. 	       & "              ""Windows PE""                (return code = 3)" _
  78. 	       & vbCrLf _
  79. 	       & "          In case of (command line) errors, the return code will be -1." _
  80. 	       & vbCrLf & vbCrLf _
  81. 	       & "Credits:  Windows PE detection based on a tip by Mitch Tulloch" _
  82. 	       & vbCrLf _
  83. 	       & "          http://techgenix.com/HowtodetectwhetheryouareinWindowsPE/" _
  84. 	       & vbCrLf & vbCrLf _
  85. 	       & "Written by Rob van der Woude" _
  86. 	       & vbCrLf _
  87. 	       & "http://www.robvanderwoude.com"
  88. 	WScript.Echo strMsg
  89. 	Abort -1
  90. End Sub
  91.  

page last uploaded: 2017-08-21, 14:26