Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.5 KiB

  1. ' Copyright (c) 1997-2001 Microsoft Corporation
  2. '***************************************************************************
  3. ' WMI script to change the OS boot default number
  4. ' based on WMI Sample Scripts (VBScript)
  5. ' Serguei Kouzmine [sergueik]
  6. '
  7. '***************************************************************************
  8. 'Initialize variables
  9. ReDim objArgs(0)
  10. 'Get the command line arguments
  11. If 0 = Wscript.arguments.count Then
  12. ' ShowUsage
  13. Else
  14. For i = 0 to Wscript.arguments.count-1
  15. ReDim Preserve objArgs(i)
  16. objArgs(i) = Wscript.arguments.Item(i)
  17. Next
  18. End If
  19. ReDim Preserve KeyedArg(2)
  20. Const lcDebug = 0
  21. Dim pbDoBoot:pbDoBoot = False
  22. Dim psHint:psHint = "SAFE"
  23. If Not IsEmpty(objArgs) Then
  24. For i = 0 to Ubound(objArgs)
  25. ParseArg(objArgs(i))
  26. If KeyedArg(0) = "l" Then
  27. psHint = KeyedArg(1)
  28. ElseIf KeyedArg(0) = "h" Then
  29. ShowUsage
  30. ElseIf KeyedArg(0) = "b" Then
  31. pbDoBoot = True
  32. End If
  33. Next
  34. End If
  35. DoChange
  36. if pbDoBoot = True Then
  37. DoBoot
  38. End If
  39. sub DoChange()
  40. Set posBootSet = GetObject("winmgmts:{(SystemEnvironment)}//./root/cimv2")._
  41. InstancesOf ("Win32_ComputerSystem")
  42. Set poRegEx = New RegExp
  43. Dim pnChange:pnChange = 0
  44. poRegEx.Global = True:poRegEx.IgnoreCase = True:poRegEx.Pattern = psHint
  45. for each poBoot in posBootSet
  46. if VBNull = VarType(poBoot.SystemStartupOptions) Then
  47. WScript.echo "Fatal: invalid data in Win32_ComputerSystem.SystemStartupOptions"
  48. WScript.echo "Giving up"
  49. WScript.Quit(1)
  50. Else
  51. Dim pdSafePos: pdSafePos = 0
  52. Dim pdCurPos : pdCurPos = 0
  53. For Each lsLabel in poBoot.SystemStartupOptions
  54. Set isMatches = poRegEx.Execute(lsLabel)
  55. If isMatches.count <> 0 Then
  56. pnSafePos = pdCurPos
  57. pnChange = 1
  58. End If
  59. pdCurPos = pdCurPos + 1
  60. Next
  61. End If
  62. Next
  63. If pnChange = 0 Then
  64. WScript.echo psHint & _
  65. " not found " &_
  66. VbCrLf &_
  67. "Giving up"
  68. WScript.Quit
  69. End If
  70. If pnSafePos = 0 Then
  71. WScript.echo "Don't need swith boot OS" &_
  72. VbCrLf
  73. Exit Sub
  74. End If
  75. Set posBootSet = Nothing
  76. Set posBootSet = GetObject("winmgmts:{impersonationLevel=impersonate,(SystemEnvironment)}")._
  77. ExecQuery("select * from Win32_ComputerSystem")
  78. for each poBoot in posBootSet
  79. poBoot.SystemStartupSetting = pnSafePos
  80. poBoot.Put_()
  81. next
  82. WScript.Echo "Boot up default OS changed to " & psHint
  83. Set posBootSet = Nothing
  84. End Sub
  85. Private Sub ParseArg(lsLabel)
  86. Set poSwitchRegEx = New RegExp
  87. Dim psMaskSwitch:psMaskSwitch = "[-/][hdb]$" 'currently don't handle all switches.
  88. poSwitchRegEx.Global = True:poSwitchRegEx.IgnoreCase = True:poSwitchRegEx.Pattern = psMaskSwitch
  89. Set poFlagRegEx = New RegExp
  90. Dim psMaskFlag:psMaskFlag = "[-/][l]:"
  91. poFlagRegEx.Global = True:poFlagRegEx.IgnoreCase = True:poFlagRegEx.Pattern = psMaskFlag
  92. Set poFullRegEx = New RegExp
  93. Dim psMaskFull:psMaskFull = psMaskFlag & "\w+"
  94. poFullRegEx.Global = True:poFullRegEx.IgnoreCase = True:poFullRegEx.Pattern = psMaskFull
  95. Dim pnChange:pnChange = 0
  96. set issSwitches = poSwitchRegEx.Execute(lsLabel)
  97. If 0 <> issSwitches.Count Then
  98. KeyedArg(0) = Mid(issSwitches(0),2,1)
  99. KeyedArg(1) = "+"
  100. ' ShowUsage
  101. Else
  102. Set isMatches = poFullRegEx.Execute(lsLabel)
  103. if 1 <> isMatches.count Then
  104. WScript.echo "Bad Argument: " & lsLabel
  105. WScript.quit(1)
  106. End If
  107. Set psFlag = poFlagRegEx.Execute(lsLabel)
  108. psRes = poFlagRegEx.Replace(lsLabel,"")
  109. KeyedArg(0) = Mid(psFlag(0),2,1)
  110. KeyedArg(1) = psRes
  111. End If
  112. End Sub
  113. Private Sub ShowUsage()
  114. Dim strFullEngineName:strFullEngineName=WScript.FullName
  115. Dim rPos:rPos = Len(strFullEngineName)
  116. Dim lPos:lPos = InStrRev(strFullEngineName, "\")
  117. strFullName = Mid(strFullEngineName, lPos + 1 , rPos - lPos)
  118. Wscript.echo "" & _
  119. "Usage:" & _
  120. VbCrLf & _
  121. Chr(9) & strFullName & Chr(32) & WScript.ScriptName & " [-l:<LABEL>] [-b] [-h]" & _
  122. VbCrLf & _
  123. Chr(9) & "Change the boot order of the BVT machine" & _
  124. VbCrLf & _
  125. "Where:" & _
  126. VbCrLf & _
  127. Chr(9) & "-l:<LABEL> boot into OS marked as <LABEL> (default is SAFE)" & _
  128. VbCrLf & _
  129. Chr(9) & "-b reboot the machine" & _
  130. VbCrLf & _
  131. Chr(9) & "-h view this message"
  132. WScript.Quit(0)
  133. End Sub
  134. private Sub DoBoot()
  135. Const lcWMIObject = "winmgmts:{impersonationLevel=impersonate, (Shutdown)}"
  136. Dim lsWMIQueryString:lsWMIQueryString = "SELECT * FROM " & _
  137. "Win32_OperatingSystem WHERE PRIMARY = true"
  138. Set loWMISet = GetObject(lcWMIObject)._
  139. ExecQuery(lsWMIQueryString, _
  140. "WQL")
  141. if lcDebug = 1 Then
  142. WScript.echo TypeName(loWMISet)
  143. WScript.echo loWMISet.Count
  144. End If
  145. For Each loWMI__ In loWMISet
  146. loWMI__.Reboot()
  147. Next
  148. End Sub