Guest User

ps1 (Microsoft provided ps1 script for extending partition)

a guest
Jul 3rd, 2024
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.62 KB | Software | 0 0
  1. Param (
  2. [Parameter(Mandatory=$false,HelpMessage="Skip confirmation")][bool]$SkipConfirmation=$false,
  3. [Parameter(Mandatory=$true,HelpMessage="Path to backup old WinRE partition content to")][string]$BackupFolder
  4. )
  5. # ------------------------------------
  6. # Helper functions
  7. # ------------------------------------
  8. # Log message
  9. function LogMessage([string]$message)
  10. {
  11. $message = "$message"
  12. Write-Host $message
  13. }
  14. # Extract numbers from string
  15. function ExtractNumbers([string]$str)
  16. {
  17. $cleanString = $str -replace "[^0-9]"
  18. return [long]$cleanString
  19. }
  20. # Display partition info using fsutil
  21. # Return an array, the first value is total size and the second value is free space
  22. function DisplayPartitionInfo([string[]]$partitionPath)
  23. {
  24. $volume = Get-WmiObject -Class Win32_Volume | Where-Object { $partitionPath -contains $_.DeviceID }
  25. LogMessage(" Partition capacity: " + $volume.Capacity)
  26. LogMessage(" Partition free space: " + $volume.FreeSpace)
  27. return $volume.Capacity, $volume.FreeSpace
  28. }
  29. # Display WinRE status
  30. function DisplayWinREStatus()
  31. {
  32. # Get WinRE partition info
  33. $WinREInfo = Reagentc /info
  34. foreach ($line in $WinREInfo)
  35. {
  36. $params = $line.Split(':')
  37. if ($params.Count -lt 2)
  38. {
  39. continue
  40. }
  41. if (($params[1].Trim() -ieq "Enabled") -Or (($params[1].Trim() -ieq "Disabled")))
  42. {
  43. $Status = $params[1].Trim() -ieq "Enabled"
  44. LogMessage($line.Trim())
  45. }
  46. if ($params[1].Trim() -like "\\?\GLOBALROOT*")
  47. {
  48. $Location = $params[1].Trim()
  49. LogMessage($line.Trim())
  50. }
  51. }
  52.  
  53. return $Status, $Location
  54. }
  55. # ------------------------------------
  56. # Main execution
  57. # ------------------------------------
  58. # Clear the error
  59. $Error.Clear()
  60. # ------------------------------------
  61. # Examining the system to collect required info
  62. # for the execution
  63. # Need to check WinRE status, collect OS and WinRE
  64. # partition info
  65. # ------------------------------------
  66. LogMessage("Start time: $([DateTime]::Now)")
  67. LogMessage("Examining the system...")
  68. $NeedShrink = $true
  69. $NeedCreateNew = $false
  70. $NeedBackup = $false
  71. # Get WinRE partition info
  72. $InitialWinREStatus = DisplayWinREStatus
  73. $WinREStatus = $InitialWinREStatus[0]
  74. $WinRELocation = $InitialWinREStatus[1]
  75. if (!$WinREStatus)
  76. {
  77. LogMessage("Error: WinRE Disabled")
  78. exit 1
  79. }
  80. # Get System directory and ReAgent xml file
  81. $system32Path = [System.Environment]::SystemDirectory
  82. LogMessage("System directory: " + $system32Path)
  83. $ReAgentXmlPath = Join-Path -Path $system32Path -ChildPath "\Recovery\ReAgent.xml"
  84. LogMessage("ReAgent xml: " + $ReAgentXmlPath)
  85. if (!(Test-Path $ReAgentXmlPath))
  86. {
  87. LogMessage("Error: ReAgent.xml cannot be found")
  88. exit 1
  89. }
  90. # Get OS partition
  91. LogMessage("")
  92. LogMessage("Collecting OS and WinRE partition info...")
  93. $OSDrive = $system32Path.Substring(0,1)
  94. $OSPartition = Get-Partition -DriveLetter $OSDrive
  95. # Get WinRE partition
  96. $WinRELocationItems = $WinRELocation.Split('\\')
  97. foreach ($item in $WinRELocationItems)
  98. {
  99. if ($item -like "harddisk*")
  100. {
  101. $OSDiskIndex = ExtractNumbers($item)
  102. }
  103. if ($item -like "partition*")
  104. {
  105. $WinREPartitionIndex = ExtractNumbers($item)
  106. }
  107. }
  108. LogMessage("OS Disk: " + $OSDiskIndex)
  109. LogMessage("OS Partition: " + $OSPartition.PartitionNumber)
  110. LogMessage("WinRE Partition: " + $WinREPartitionIndex)
  111. $WinREPartition = Get-Partition -DiskNumber $OSDiskIndex -PartitionNumber $WinREPartitionIndex
  112. $diskInfo = Get-Disk -number $OSDiskIndex
  113. $diskType = $diskInfo.PartitionStyle
  114. LogMessage("Disk PartitionStyle: " + $diskType)
  115. # Display WinRE partition size info
  116. LogMessage("WinRE partition size info")
  117. $WinREPartitionSizeInfo = DisplayPartitionInfo($WinREPartition.AccessPaths)
  118. LogMessage("WinRE Partition Offset: " + $WinREPartition.Offset)
  119. LogMessage("WinRE Partition Type: " + $WinREPartition.Type)
  120. LogMessage("OS partition size: " + $OSPartition.Size)
  121. LogMessage("OS partition Offset: " + $OSPartition.Offset)
  122. $OSPartitionEnds = $OSPartition.Offset + $OSPartition.Size
  123. LogMessage("OS partition ends at: " + $OSPartitionEnds)
  124. LogMessage("WinRE partition starts at: " + $WinREPartition.Offset)
  125. $WinREIsOnSystemPartition = $false
  126. if ($diskType -ieq "MBR")
  127. {
  128. if ($WinREPartition.IsActive)
  129. {
  130. LogMessage("WinRE is on System partition")
  131. $WinREIsOnSystemPartition = $true
  132. }
  133. }
  134. if ($diskType -ieq "GPT")
  135. {
  136. if ($WinREPartition.Type -ieq "System")
  137. {
  138. LogMessage("WinRE is on System partition")
  139. $WinREIsOnSystemPartition = $true
  140. }
  141. }
  142. # Checking the BackupFolder parameter
  143. if ($PSBoundParameters.ContainsKey('BackupFolder'))
  144. {
  145. LogMessage("")
  146. LogMessage("Backup Directory: [" + $BackupFolder + "]")
  147.  
  148. $Needbackup = $true
  149.  
  150. if ($WinREIsOnSystemPartition)
  151. {
  152. $Needbackup = $false
  153. LogMessage("WinRE is on System partition which will be preserved. No need to backup content")
  154. }
  155. else
  156. {
  157. if (Test-path $BackupFolder)
  158. {
  159. $items = Get-ChildItem -Path $BackupFolder
  160. if ($items)
  161. {
  162. LogMessage("Error: Existing backup directory is not empty")
  163. exit 1
  164. }
  165. }
  166. else
  167. {
  168. LogMessage("Creating backup directory...")
  169. try
  170. {
  171. $item = New-Item -Path $BackupFolder -ItemType Directory -ErrorAction Stop
  172. if ($item)
  173. {
  174. LogMessage("Backup directory created")
  175. }
  176. else
  177. {
  178. LogMessage("Error: Failed to create backup directory [" + $BackupFolder + "]")
  179. exit 1
  180. }
  181. } catch
  182. {
  183. LogMessage("Error: An error occurred: $_")
  184. exit 1
  185. }
  186. }
  187. }
  188. }
  189. # ------------------------------------
  190. # Verify whether we meet requirements of execution
  191. # - WinRE cannot be on OS partition for the extension
  192. # - WinRE partition must be the next partition after OS partition
  193. # - If WinRE partition already have >=250MB free space, no need to do repartition
  194. # - If there is enough unallocated space to grow the WinRE partition size, skip shrinking OS
  195. #
  196. # However, if the WinRE partition is before the OS partition, there is no chance to extend it
  197. # As a result, it's better to create a new WinRE partition after the OS partition
  198. # ------------------------------------
  199. # Perform a few checks
  200. LogMessage("")
  201. LogMessage("Verifying if the WinRE partition needs to be extended or not...")
  202. if (!(($diskType -ieq "MBR") -Or ($diskType -ieq "GPT")))
  203. {
  204. LogMessage("Error: Got an unexpected disk partition style: " +$diskType)
  205. exit 1
  206. }
  207. # WinRE partition must be after OS partition for the repartition
  208. if ($WinREPartitionIndex -eq $OSPartition.PartitionNumber)
  209. {
  210. LogMessage("WinRE and OS are on the same partition, should not perform extension")
  211. exit 0
  212. }
  213. $supportedSize = Get-PartitionSupportedSize -DriveLetter $OSDrive
  214. # if there is enough free space, skip extension
  215. if ($WinREPartitionSizeInfo[1] -ge 250MB)
  216. {
  217. LogMessage("More than 250 MB of free space was detected in the WinRE partition, there is no need to extend the partition")
  218. exit 0
  219. }
  220. if ($WinREPartition.Offset -lt $OSPartitionEnds)
  221. {
  222. LogMessage("WinRE partition is not after OS partition, cannot perform extension")
  223. LogMessage("Need to create a new WinRE partition after OS partition")
  224. $NeedCreateNew = $true
  225. $NeedShrink = $true
  226.  
  227. # Calculate the size of repartition
  228. # Will create a new WinRE partition with current WinRE partition size + 250 MB
  229. # The OS partition size will be shrunk by the new WinRE partition size
  230. $targetWinREPartitionSize = $WinREPartitionSizeInfo[0] + 250MB
  231. $shrinkSize = [Math]::Ceiling($targetWinREPartitionSize / 1MB) * 1MB
  232. $targetOSPartitionSize = $OSPartition.Size - $shrinkSize
  233. if ($targetOSPartitionSize -lt $supportedSize.SizeMin)
  234. {
  235. LogMessage("Error: The target OS partition size after shrinking is smaller than the supported minimum size, cannot perform the repartition")
  236. exit 1
  237. }
  238. }
  239. else
  240. {
  241. if ($WinREIsOnSystemPartition)
  242. {
  243. LogMessage("WinRE parititon is after the OS partition and it's also System partition")
  244. LogMessage("Error: Got unexpected disk layout, cannot proceed")
  245. exit 1
  246. }
  247. if (!($WinREPartitionIndex -eq ($OSPartition.PartitionNumber + 1)))
  248. {
  249. LogMessage("Error: WinRE partition is not right after the OS partition, cannot extend WinRE partition")
  250. exit 1
  251. }
  252. # Calculate the size of repartition
  253. # Will shrink OS partitition by 250 MB
  254. $shrinkSize = 250MB
  255. $targetOSPartitionSize = $OSPartition.Size - $shrinkSize
  256. $targetWinREPartitionSize = $WinREPartitionSizeInfo[0] + 250MB
  257. $UnallocatedSpace = $WinREPartition.Offset - $OSPartitionEnds;
  258. # If there is unallocated space, consider using it
  259. if ($UnallocatedSpace -ge 250MB)
  260. {
  261. $UnallocatedSpace = $WinREPartition.Offset - $OSPartitionEnds;
  262. LogMessage("Found unallocated space between OS and WinRE partition: " + $UnallocatedSpace)
  263. LogMessage("There is already enough space to extend WinRE partition without shrinking the OS partition")
  264. $NeedShrink = $false
  265. $targetOSPartitionSize = 0
  266. }
  267. else
  268. {
  269. $shrinkSize = [Math]::Ceiling((250MB - $UnallocatedSpace)/ 1MB) * 1MB
  270. if ($shrinkSize > 250MB)
  271. {
  272. $shrinkSize = 250MB
  273. }
  274. $targetOSPartitionSize = $OSPartition.Size - $shrinkSize
  275. if ($targetOSPartitionSize -lt $supportedSize.SizeMin)
  276. {
  277. LogMessage("Error: The target OS partition size after shrinking is smaller than the supported minimum size, cannot perform the repartition")
  278. exit 1
  279. }
  280. }
  281. }
  282. # ------------------------------------
  283. # Report execution plan and ask for user confirmation to continue
  284. # ------------------------------------
  285. # Report the changes planned to be executed, waiting for user confirmation
  286. LogMessage("")
  287. LogMessage("Summary of proposed changes")
  288. if ($NeedCreateNew)
  289. {
  290. LogMessage("Note: WinRE partition is before OS partition, need to create a new WinRE partition after OS partition")
  291. LogMessage("Will shrink OS partition by " + $shrinkSize)
  292. LogMessage(" Current OS partition size: " + $OSPartition.Size)
  293. LogMessage(" Target OS partition size after shrinking: " + $targetOSPartitionSize)
  294. LogMessage("New WinRE partition will be created with size: ", $targetWinREPartitionSize)
  295. if ($WinREIsOnSystemPartition)
  296. {
  297. LogMessage("Existing WinRE partition is also system partition, it will be preserved")
  298. }
  299. else
  300. {
  301. LogMessage("Existing WinRE partition will be deleted")
  302. LogMessage(" WinRE partition: Disk [" + $OSDiskIndex + "] Partition [" + $WinREPartitionIndex + "]")
  303. LogMessage(" Current WinRE partition size: " + $WinREPartitionSizeInfo[0])
  304. }
  305. }
  306. else
  307. {
  308. if ($NeedShrink)
  309. {
  310. LogMessage("Will shrink OS partition by " + $shrinkSize)
  311. LogMessage(" Current OS partition size: " + $OSPartition.Size)
  312. LogMessage(" Target OS partition size after shrinking: " + $targetOSPartitionSize)
  313. if ($UnallocatedSpace -ge 0)
  314. {
  315. LogMessage("Unallocated space between OS and WinRE partition that will be used towards the new WinRE partition: " + $UnallocatedSpace)
  316. }
  317. }
  318. else
  319. {
  320. LogMessage("Will use 250MB from unallocated space between OS and WinRE partition")
  321. }
  322. LogMessage("Will extend WinRE partition size by 250MB")
  323. LogMessage(" WinRE partition: Disk [" + $OSDiskIndex + "] Partition [" + $WinREPartitionIndex + "]")
  324. LogMessage(" Current WinRE partition size: " + $WinREPartitionSizeInfo[0])
  325. LogMessage(" New WinRE partition size: " + $targetWinREPartitionSize)
  326. LogMessage("WinRE will be temporarily disabled before extending the WinRE partition and enabled automatically in the end")
  327. if ($UnallocatedSpace -ge 100MB)
  328. {
  329. LogMessage("Warning: More than 100MB of unallocated space was detected between the OS and WinRE partitions")
  330. LogMessage("Would you like to proceed by using the unallocated space between the OS and the WinRE partitions?")
  331. }
  332. }
  333. if ($Needbackup)
  334. {
  335. LogMessage("")
  336. LogMessage("The contents of the old WinRE partition will be backed up to [" + $BackupFolder + "]")
  337. }
  338. LogMessage("")
  339. LogMessage("Please reboot the device before running this script to ensure any pending partition actions are finalized")
  340. LogMessage("")
  341. if ($SkipConfirmation)
  342. {
  343. LogMessage("User chose to skip confirmation")
  344. LogMessage("Proceeding with changes...")
  345. }
  346. else
  347. {
  348. $userInput = Read-Host -Prompt "Would you like to proceed? Y for Yes and N for No"
  349.  
  350. if ($userInput -ieq "Y")
  351. {
  352. LogMessage("Proceeding with changes...")
  353. }
  354. elseif ($userInput -ieq "N")
  355. {
  356. LogMessage("Canceling based on user request, no changes were made to the system")
  357. exit 0
  358. }
  359. else
  360. {
  361. LogMessage("Error: Unexpected user input: [" + $userInput + "]")
  362. exit 0
  363. }
  364. }
  365. LogMessage("")
  366. LogMessage("Note: To prevent unexpected results, please do not interrupt the execution or restart your system")
  367. # ------------------------------------
  368. # Do the actual execution
  369. # The main flow is:
  370. # 1. Check whether ReAgent.xml has stage location and clear it for repartiion
  371. # 2. Disable WinRE as WinRE partition will be deleted
  372. # 3. Perform the repartition to create a larger WinRE partition
  373. # 4. Re-enable WinRE
  374. # ------------------------------------
  375. LogMessage("")
  376. # Load ReAgent.xml to clear Stage location
  377. LogMessage("Loading [" + $ReAgentXmlPath + "] ...")
  378. $xml = [xml](Get-Content -Path $ReAgentXmlPath)
  379. $node = $xml.WindowsRE.ImageLocation
  380. if (($node.path -eq "") -And ($node.guid -eq "{00000000-0000-0000-0000-000000000000}") -And ($node.offset -eq "0") -And ($node.id -eq "0"))
  381. {
  382. LogMessage("Stage location info is empty")
  383. }
  384. else
  385. {
  386. LogMessage("Clearing stage location info...")
  387. $node.path = ""
  388. $node.offset = "0"
  389. $node.guid= "{00000000-0000-0000-0000-000000000000}"
  390. $node.id="0"
  391. # Save the change
  392. LogMessage("Saving changes to [" + $ReAgentXmlPath + "]...")
  393. $xml.Save($ReAgentXmlPath)
  394. }
  395. # Disable WinRE
  396. LogMessage("Disabling WinRE...")
  397. reagentc /disable
  398. if (!($LASTEXITCODE -eq 0))
  399. {
  400. LogMessage("Warning: encountered an error when disabling WinRE: " + $LASTEXITCODE)
  401. exit $LASTEXITCODE
  402. }
  403. # Verify WinRE is under C:\Windows\System32\Recovery\WinRE.wim
  404. $disableWinREPath = Join-Path -Path $system32Path -ChildPath "\Recovery\WinRE.wim"
  405. LogMessage("Verifying that WinRE wim exists in downlevel at default location")
  406. if (!(Test-Path $disableWinREPath))
  407. {
  408. LogMessage("Error: Cannot find " + $disableWinREPath)
  409.  
  410. # Re-enable WinRE
  411. LogMessage("Re-enabling WinRE on error...")
  412. reagentc /enable
  413. if (!($LASTEXITCODE -eq 0))
  414. {
  415. LogMessage("Warning: encountered an error when enabling WinRE: " + $LASTEXITCODE)
  416. }
  417. exit 1
  418. }
  419. # ------------------------------------
  420. # Perform the repartition
  421. # 1. Resize the OS partition
  422. # 2. Delete the WinRE partition
  423. # 3. Create a new WinRE partition
  424. # ------------------------------------
  425. LogMessage("Performing repartition to extend the WinRE partition ...")
  426. # 1. Resize the OS partition
  427. if ($NeedShrink)
  428. {
  429. LogMessage("Shrinking the OS partition to create a larger WinRE partition")
  430. LogMessage("Resizing the OS partition to: [" + $targetOSPartitionSize + "]...")
  431. Resize-Partition -DriveLetter $OSDrive -Size $targetOSPartitionSize
  432. if ($Error.Count -gt 0) {
  433. LogMessage("Error: Resize-Partition encountered errors: " + $Error[0].Exception.Message)
  434.  
  435. # Re-enable WinRE
  436. LogMessage("Re-enabling WinRE on error...")
  437. reagentc /enable
  438. if (!($LASTEXITCODE -eq 0))
  439. {
  440. LogMessage("Warning: encountered an error when enabling WinRE: " + $LASTEXITCODE)
  441. }
  442. exit 1
  443. }
  444. $OSPartitionAfterShrink = Get-Partition -DriveLetter $OSDrive
  445. LogMessage("Target partition size: " + $targetOSPartitionSize)
  446. LogMessage("Size of OS partition after shrinking: " + $OSPartitionAfterShrink.Size)
  447. }
  448. # 2. Delete the WinRE partition
  449. LogMessage("")
  450. if ($WinREIsOnSystemPartition)
  451. {
  452. LogMessage("Existing WinRE partition is System partition, skipping deletion")
  453. }
  454. else
  455. {
  456. # If requested by user, backup rest of the content on WinRE partition to backup directory
  457. if ($Needbackup)
  458. {
  459. $sourcePath = $WinREPartition.AccessPaths[0]
  460. LogMessage("Copying content on WinRE partition from [" + $sourcePath + "] to [" + $BackupFolder + "]...")
  461.  
  462. # Copy-Item may have access issue with certain system folders, enumerate the children items and exlcude them
  463. $items = Get-ChildItem -LiteralPath $sourcePath -Force
  464. foreach ($item in $items)
  465. {
  466. if ($item.Name -ieq "System Volume Information")
  467. {
  468. continue
  469. }
  470. $sourceItemPath = Join-Path -Path $sourcePath -ChildPath $item.Name
  471. $destItemPath = Join-Path -Path $BackupFolder -ChildPath $item.Name
  472. try
  473. {
  474. LogMessage("Copying [" + $sourceItemPath + "] to [" + $destItemPath + "]...")
  475. Copy-Item -LiteralPath $sourceItemPath -Destination $destItemPath -Recurse -Force
  476. } catch
  477. {
  478. LogMessage("Error: An error occurred during copy: $_")
  479. exit 1
  480. }
  481. }
  482.  
  483. LogMessage("Backup completed")
  484. LogMessage("")
  485. }
  486. LogMessage("Deleting WinRE partition: Disk [" + $OSDiskIndex + "] Partition [" + $WinREPartitionIndex + "]...")
  487. Remove-Partition -DiskNumber $OSDiskIndex -PartitionNumber $WinREPartitionIndex -Confirm:$false
  488. if ($Error.Count -gt 0) {
  489. LogMessage("Error: Remove-Partition encountered errors: " + $Error[0].Exception.Message)
  490. exit 1
  491. }
  492. }
  493. # A short sleep for the partition change
  494. Sleep 5
  495. # 3. Create a new WinRE partition
  496. LogMessage("")
  497. LogMessage("Creating new WinRE partition...")
  498. LogMessage("Target size: " + $targetWinREPartitionSize)
  499. if ($diskType -ieq "GPT")
  500. {
  501. $partition = New-Partition -DiskNumber $OSDiskIndex -Size $targetWinREPartitionSize -GptType "{de94bba4-06d1-4d40-a16a-bfd50179d6ac}"
  502.  
  503. $newPartitionIndex = $partition.PartitionNumber
  504. # A short sleep to make sure the partition is ready for formatting
  505. Sleep 2
  506. LogMessage("Formating the partition...")
  507. $result = Format-Volume -Partition $partition -FileSystem NTFS -Confirm:$false
  508. if ($Error.Count -gt 0) {
  509. LogMessage("Error: Format-Volume encountered errors: " + $Error[0].Exception.Message)
  510. exit 1
  511. }
  512. }
  513. else
  514. {
  515. #$partition = New-Partition -DiskNumber $OSDiskIndex -Size $targetWinREPartitionSize -MbrType 0x27
  516. $targetWinREPartitionSizeInMb = [int]($targetWinREPartitionSize/1MB)
  517. $diskpartScript =
  518. @"
  519. select disk $OSDiskIndex
  520. create partition primary size=$targetWinREPartitionSizeInMb id=27
  521. format quick fs=ntfs label="Recovery"
  522. set id=27
  523. "@
  524. $TempPath = $env:Temp
  525. $diskpartSciptFile = Join-Path -Path $TempPath -ChildPath "\ExtendWinRE_MBR_PowershellScript.txt"
  526.  
  527. LogMessage("Creating temporary diskpart script to create Recovery partition on MBR disk...")
  528. LogMessage("Temporary diskpart script file: " + $diskpartSciptFile)
  529. $diskpartScript | Out-File -FilePath $diskpartSciptFile -Encoding ascii
  530.  
  531. LogMessage("Executing diskpart script...")
  532. try
  533. {
  534. $diskpartOutput = diskpart /s $diskpartSciptFile
  535.  
  536. if ($diskpartOutput -match "DiskPart successfully")
  537. {
  538. LogMessage("Diskpart script executed successfully")
  539. }
  540. else
  541. {
  542. LogMessage("Error executing diskpart script:" + $diskpartOutput)
  543. exit 1
  544. }
  545. LogMessage("Deleting temporary diskpart script file...")
  546. Remove-Item $diskpartSciptFile
  547. }
  548. catch
  549. {
  550. LogMessage("Error executing diskpart script: $_")
  551. exit 1
  552. }
  553.  
  554. $vol = Get-Volume -FileSystemLabel "Recovery"
  555. $newPartitionIndex = (Get-Partition | Where-Object { $_.AccessPaths -contains $vol.Path } ).PartitionNumber
  556. }
  557. if ($Error.Count -gt 0)
  558. {
  559. LogMessage("Error: New-Partition encountered errors: " + $Error[0].Exception.Message)
  560. exit 1
  561. }
  562. LogMessage("New Partition index: " + $newPartitionIndex)
  563. # Re-enable WinRE
  564. LogMessage("Re-enabling WinRE...")
  565. reagentc /enable
  566. if (!($LASTEXITCODE -eq 0))
  567. {
  568. LogMessage("Warning: encountered an error when enabling WinRE: " + $LASTEXITCODE)
  569. exit $LASTEXITCODE
  570. }
  571. # In the end, Display WinRE status to verify WinRE is enabled correctly
  572. LogMessage("")
  573. LogMessage("WinRE Information:")
  574. $FinalWinREStatus = DisplayWinREStatus
  575. $WinREStatus = $FinalWinREStatus[0]
  576. $WinRELocation = $FinalWinREStatus[1]
  577. if (!$WinREStatus)
  578. {
  579. LogMessage("Warning: WinRE Disabled")
  580. }
  581. $WinRELocationItems = $WinRELocation.Split('\\')
  582. foreach ($item in $WinRELocationItems)
  583. {
  584. if ($item -like "partition*")
  585. {
  586. $WinREPartitionIndex = ExtractNumbers($item)
  587. }
  588. }
  589. LogMessage("WinRE Partition Index: " + $WinREPartitionIndex)
  590. $WinREPartition = Get-Partition -DiskNumber $OSDiskIndex -PartitionNumber $WinREPartitionIndex
  591. $WinREPartitionSizeInfoAfter = DisplayPartitionInfo($WinREPartition.AccessPaths)
  592. LogMessage("")
  593. LogMessage("OS Information:")
  594. $OSPartition = Get-Partition -DriveLetter $OSDrive
  595. LogMessage("OS partition size: " + $OSPartition.Size)
  596. LogMessage("OS partition Offset: " + $OSPartition.Offset)
  597. if (!($WinREPartitionIndex -eq $newPartitionIndex))
  598. {
  599. LogMessage("Warning: WinRE is installed to partition [" + $WinREPartitionIndex +"], but the newly created Recovery partition is [" + $newPartitionIndex + "]")
  600. }
  601. LogMessage("End time: $([DateTime]::Now)")
  602. if ($NeedBackup)
  603. {
  604. LogMessage("")
  605. LogMessage("The contents of the old WinRE partition has been backed up to [" + $BackupFolder + "]")
  606. }
  607. LogMessage("")
  608. LogMessage("Successfully completed the operation")
Add Comment
Please, Sign In to add comment