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.

518 lines
13 KiB

  1. <?XML version="1.0" ?>
  2. <package>
  3. <comment>
  4. Used in International and US BVT.
  5. The script is able to manage remote box as well as local machine.
  6. </comment>
  7. <comment>
  8. Purpose:
  9. ========
  10. The script connects to the chosen BVT machine,
  11. lists the available boot options to identify the safe boot.
  12. If found, the safe boot option is made the next default boot option.
  13. Optionally, the BVT machine is rebooted.
  14. The current version of the script is designed to provide remote execution only.
  15. The BVT machine name is supposed to be meaningful and to be specified on command line.
  16. The script is not blocking.
  17. Errors of no BVT machine found or no valid user account logged are
  18. caught and displayed.
  19. An attempt to connect to the BVT machine WBEM provider takes approximately
  20. one minute when the server is unavailable or not responding.
  21. There is currently no inquiry of the state of the BVT machine.
  22. BVT machine would be rebooted even if in the middle of BVT test.
  23. The remote execution is based on the assumption the ntdev/winbld user is
  24. currently in ADMINISTRATORS local group to the BVT machine. the user does not have
  25. to be logged to BVT machine.
  26. </comment>
  27. <comment>
  28. (c) 2001 Microsoft Corporation. All rights reserved.
  29. Version: 1.00 09/13/2001 : Serguei Kouzmine
  30. [email protected]
  31. </comment>
  32. <job>
  33. <reference guid="{565783C6-CB41-11D1-8B02-00600806D9B6}" version="1.1"/>
  34. <resource id="cDebug">0</resource>
  35. <resource id="Label">SAFE</resource>
  36. <resource id="Usage">
  37. Usage:
  38. %SELF% /b:%MACHINE% [/r] [/h] [/l:%LABEL%] /u:%USER% /p:%PASSWORD%
  39. Where:
  40. /b:%MACHINE% Change the boot order of the specified BVT machine.
  41. /l:%LABEL% boot into OS marked as %LABEL% (default is SAFE)
  42. /r reboot the machine, specify it only if %MACHINE% defined.
  43. /d enable debug information
  44. /u user to impersonate
  45. /p password if user differs from the currently logged on
  46. /h view this message
  47. Example:
  48. %SELF% /r
  49. %SELF% /b:I64BT0041 /r
  50. %SELF% /b:i32bt0031 /l:"safe boot" /d
  51. </resource>
  52. <resource id="user">ntdev\winbld</resource>
  53. <resource id="password">XXXXXXXX</resource>
  54. <script language="VBScript">
  55. <![CDATA[
  56. option explicit
  57. Function ReadResource(sPresId)
  58. On Error resume next
  59. Dim WshShell
  60. Set WshShell = CreateObject("WScript.Shell")
  61. Dim sPRes, asRes, uRes, tsRes
  62. sPREs = GetResource(sPresId)
  63. If Err.number <> 0 Then
  64. If cDebug Then
  65. Wscript.echo err.number & " " & err.description
  66. ' The Error description are not informative when failed getresource
  67. IF Err.number = 7 Then
  68. Wscript.echo chr(9) & "Null resource: " & _
  69. sPresId
  70. End If
  71. IF Err.number = 5 Then
  72. Wscript.echo chr(9) & "Undefined resource: " & _
  73. sPresId
  74. End If
  75. Wscript.quit
  76. End If
  77. End If
  78. asRes = Split(WshShell.ExpandEnvironmentStrings(sPRes), VBNEWLINE)
  79. Set ures = CreateObject("Scripting.Dictionary")
  80. For Each tsRes in asRes
  81. If 0 <> Len(tsRes) Then
  82. uRes(tsRes) = 1
  83. End If
  84. Next
  85. Dim tResource
  86. tResource = uRes.Keys
  87. If uBound(tResource) = 0 Then
  88. ReadResource = tResource(0)
  89. Else
  90. ReadResource = tResource
  91. End If
  92. Set WshShell = Nothing
  93. Set uRes = Nothing
  94. End Function
  95. sub showUsage
  96. Const VBEXPECTED = 5
  97. on error resume next
  98. Dim oGenRplaceVarNamXp
  99. Dim oExRplaceVarNamXp
  100. Dim aoRplaceMatches, oMatch, sMatch, sLabel, sResource
  101. Set oGenRplaceVarNamXp = new RegExp
  102. Set oExRplaceVarNamXp = new RegExp
  103. With oGenRplaceVarNamXp
  104. .Global = True
  105. .IgnoreCase = True
  106. .Pattern = "%\w+%"
  107. End With
  108. With oExRplaceVarNamXp
  109. .Global = True
  110. .IgnoreCase = True
  111. .Pattern = "%\w+%"
  112. End With
  113. Dim sLine,asTest
  114. asTest = ReadResource("Usage")
  115. For Each sLine in asTest
  116. Set aoRplaceMatches = oGenRplaceVarNamXp.Execute(sLine)
  117. For Each oMatch in aoRplaceMatches
  118. sMatch = oMatch.value
  119. sMatch = UCase(Mid(sMatch, 2, Len(sMatch)-2))
  120. sLabel = LCase(sMatch)
  121. Err.clear
  122. sResource = GetResource(sLabel)
  123. If err = 0 Then
  124. sMatch = sResource
  125. ElseIf err = VBEXPECTED Then
  126. sMatch = "<" & sMatch & ">"
  127. Else
  128. Raise Err
  129. End If
  130. oExRplaceVarNamXp.Pattern = oMatch.value
  131. sLine = oExRplaceVarNamXp.Replace(sLine, sMatch)
  132. Next
  133. WScript.echo sLine
  134. Next
  135. WSCript.Quit(0)
  136. End sub
  137. ]]>
  138. </script>
  139. <script language="VBScript">
  140. <![CDATA[
  141. Option Explicit
  142. Dim spUser, spPassword, opNetwork
  143. spUser = CStr(GetResource("user"))
  144. spPassword = CStr(GetResource("password"))
  145. set opNetwork=Wscript.CreateObject("Wscript.network")
  146. Dim spDefaultComputerName, spDefaultUser
  147. Dim cDebug
  148. Dim KeyedArg
  149. Dim tPLocator
  150. Dim spComputerName
  151. Dim tPService
  152. Dim sPQryStr
  153. Dim tPBootSet, tPBoot
  154. Dim sPSelfScript
  155. Dim sPSafeBootLabel, uPSafeBootLabel
  156. Dim sPDoBoot
  157. Dim psMaskSwitch
  158. Dim npChange
  159. Dim nTSafePos
  160. Dim nTCurPos
  161. Dim poKnownSwitchRegEx, psMaskKnownSwitch
  162. Dim poBadSwitchRegEx, psMaskBadSwitch
  163. Dim issSwitches
  164. Dim nLCnt
  165. Dim tpBootOptRegEx
  166. Dim sTStartupOption
  167. Dim isMatches
  168. ' On Error resume next
  169. cDebug = cBool(GetResource("cDebug"))
  170. ReDim objArgs(0)
  171. sPSelfScript = WScript.ScriptName
  172. Set poKnownSwitchRegEx = New RegExp
  173. Set poBadSwitchRegEx = New RegExp
  174. psMaskKnownSwitch = "[-/][hdrup]$"
  175. poKnownSwitchRegEx.Global = True
  176. poKnownSwitchRegEx.IgnoreCase = True
  177. poKnownSwitchRegEx.Pattern = psMaskKnownSwitch
  178. psMaskBadSwitch = "[-/][^hdrlbup]"
  179. poBadSwitchRegEx.Global = True
  180. poBadSwitchRegEx.IgnoreCase = True
  181. poBadSwitchRegEx.Pattern = psMaskBadSwitch
  182. For nLCnt = 0 to Wscript.arguments.count-1
  183. ReDim Preserve objArgs(nLCnt)
  184. objArgs(nLCnt) = Wscript.arguments.Item(nLCnt)
  185. Next
  186. If Not IsEmpty(objArgs) Then
  187. For nLCnt = 0 to Ubound(objArgs)
  188. set issSwitches = poBadSwitchRegEx.Execute(objArgs(nLCnt))
  189. If 0 <> issSwitches.Count Then
  190. WScript.StdErr.Write _
  191. sPSelfScript & _
  192. ": unrecognized command line argument: " & _
  193. objArgs(nLCnt) & _
  194. VBCRLF
  195. ShowUsage
  196. End If
  197. set issSwitches = Nothing
  198. set issSwitches = poKnownSwitchRegEx.Execute(objArgs(nLCnt))
  199. If 0 <> issSwitches.Count Then
  200. KeyedArg = Mid(issSwitches(0),2,1)
  201. If Ucase(KeyedArg) = "H" Then
  202. ShowUsage
  203. End If
  204. If Ucase(KeyedArg) = "R" Then
  205. sPDoBoot = 1
  206. End If
  207. If Ucase(KeyedArg) = "D" Then
  208. cDebug = True
  209. End If
  210. End If
  211. Next
  212. End If
  213. If cDebug Then
  214. WScript.echo "cDebug=" & cDebug
  215. End If
  216. spComputerName = ""
  217. spComputerName = WScript.Arguments.Named("b")
  218. If IsEmpty(spComputerName) Then
  219. spDefaultComputerName=opNetwork.computername
  220. spComputerName = spDefaultComputerName
  221. sPDoBoot = 1
  222. End If
  223. sPSafeBootLabel = GetResource("Label")
  224. uPSafeBootLabel = WScript.Arguments.Named("l")
  225. If Not IsEmpty(uPSafeBootLabel) Then
  226. sPSafeBootLabel = Ucase(uPSafeBootLabel)
  227. End If
  228. If cDebug Then
  229. WScript.echo _
  230. "Checking boot option labeled: """ & _
  231. sPSafeBootLabel & _
  232. """"
  233. End If
  234. Dim siUser
  235. siUser = WSCript.Arguments.Named("u")
  236. If siUser <> "" Then
  237. spUser = siUser
  238. end If
  239. spDefaultUser = opNetwork.UserName
  240. WSCript.echo spDefaultUser
  241. Dim siPassword
  242. siPassword = WSCript.Arguments.Named("p")
  243. If spPassword <> "" Then
  244. spPassword = siPassword
  245. end If
  246. Err.Clear
  247. Set tPlocator = CreateObject("WbemScripting.SWbemLocator")
  248. tPlocator.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate
  249. ' wbemImpersonationLevelImpersonate constant: 3
  250. ' The symbolic constant name is not to be used here. The code
  251. ' tPlocator.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate
  252. ' does not work.
  253. tPlocator.Security_.Privileges.AddAsString("SeRemoteShutdownPrivilege")
  254. tPlocator.Security_.Privileges.AddAsString("SeShutdownPrivilege")
  255. tPlocator.Security_.Privileges.AddAsString("SeSystemEnvironmentPrivilege")
  256. If spComputerName = spDefaultComputerName Then
  257. Set tPService = tPlocator.ConnectServer(spDefaultComputerName, "root\CimV2")
  258. Else
  259. If spUser <> spDefaultUser Then
  260. Set tPService = tPlocator.ConnectServer(spComputerName, "root\CimV2", spUser, spPassword)
  261. Else
  262. Set tPService = tPlocator.ConnectServer(spComputerName, "root\CimV2")
  263. End If
  264. End If
  265. If Err Then
  266. WScript.Stderr.write "WBEM provider at " & _
  267. spComputerName & _
  268. ".\\root\cimv2 " & _
  269. Err.Number & _
  270. Chr(09) & _
  271. Err.Description
  272. WScript.Quit
  273. End If
  274. Err.Clear
  275. Set tPBoot = tPService.Get("Win32_ComputerSystem.Name="""& _
  276. spComputerName & _
  277. """")
  278. If Err Then
  279. WScript.Stderr.write "WBEM provider at " & _
  280. spComputerName & _
  281. ".\\root\cimv2 " & _
  282. Err.Number & _
  283. Chr(09) & _
  284. Err.Description
  285. WScript.Quit
  286. End If
  287. If cDebug Then
  288. wscript.echo "Looking " & _
  289. sPSafeBootLabel & _
  290. " Boot option of " & _
  291. tPBoot.Caption
  292. End If
  293. Set tPBoot = Nothing
  294. Set tPBootSet = tPService._
  295. InstancesOf ("Win32_ComputerSystem")
  296. npChange = 0
  297. Set tpBootOptRegEx = New RegExp
  298. tpBootOptRegEx.Global = True
  299. tpBootOptRegEx.IgnoreCase = True
  300. tpBootOptRegEx.Pattern = sPSafeBootLabel
  301. For Each tPBoot in tPBootSet
  302. If cDebug = True Then
  303. wscript.echo "Obtained " & _
  304. tPBoot.GetObjectText_
  305. End If
  306. if VBNull = VarType(tPBoot.SystemStartupOptions) Then
  307. WScript.echo "Fatal: invalid data in Win32_ComputerSystem.SystemStartupOptions"
  308. WScript.echo "Giving up"
  309. WScript.Quit(1)
  310. Else
  311. nTSafePos = 0
  312. nTCurPos = 0
  313. For Each sTStartupOption in tPBoot.SystemStartupOptions
  314. If cDebug = True Then
  315. wscript.echo "Checking in SystemStartupOptions:" & _
  316. sTStartupOption
  317. End If
  318. Set isMatches = tpBootOptRegEx.Execute(sTStartupOption)
  319. If isMatches.count <> 0 Then
  320. nTSafePos = nTCurPos
  321. npChange = 1
  322. End If
  323. nTCurPos = nTCurPos + 1
  324. Next
  325. End If
  326. Next
  327. ShowStatus npChange, nTSafePos
  328. If nTSafePos <> 0 Then
  329. Set tPBootSet = Nothing
  330. Set tPBootSet = tPService._
  331. ExecQuery("select * from Win32_ComputerSystem")
  332. for each tPBoot in tPBootSet
  333. tPBoot.SystemStartupSetting = nTSafePos
  334. tPBoot.Put_()
  335. next
  336. If cDebug Then
  337. WScript.Echo "Boot up default OS changed to " & sPSafeBootLabel
  338. End If
  339. Set tPBootSet = Nothing
  340. End If
  341. If sPDoBoot Then
  342. If cDebug Then
  343. WSCript.echo "Booting " & spComputerName
  344. End If
  345. sPQryStr = "SELECT * FROM " & _
  346. "Win32_OperatingSystem WHERE PRIMARY = true"
  347. Set tPBootSet = tPService._
  348. ExecQuery(sPQryStr, _
  349. "WQL")
  350. if cDebug = True Then
  351. WScript.echo TypeName(tPBootSet)
  352. WScript.echo tPBootSet.Count
  353. End If
  354. For Each tPBoot In tPBootSet
  355. tPBoot.Reboot()
  356. Next
  357. Set tPBootSet = Nothing
  358. End If
  359. WScript.Quit
  360. Sub ShowStatus(npChange, nTSafePos)
  361. If npChange = 0 Then
  362. If cDebug Then
  363. WScript.echo sPSafeBootLabel & _
  364. " not found " &_
  365. VbCrLf &_
  366. "Giving up"
  367. End If
  368. WScript.Quit
  369. End If
  370. If nTSafePos = 0 Then
  371. WScript.echo "Don't need switch boot OS:" & _
  372. VbCrLf & Chr(09) & sPSafeBootLabel & _
  373. " is the current default" & VbCrLf
  374. Exit Sub
  375. End If
  376. If nTSafePos <> 0 Then
  377. If cDebug Then
  378. WScript.echo "Found " & _
  379. sPSafeBootLabel & _
  380. " Boot option (" & _
  381. nTSafePos & _
  382. "), switching to it"
  383. End If
  384. End If
  385. End Sub
  386. ]]>
  387. </script>
  388. </job>
  389. </package>