Created
January 24, 2024 23:20
-
-
Save MyITGuy/867a8e4d4143b1429284288f6c1c8595 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-WinREPartitionInfo { | |
[CmdletBinding()] | |
param() | |
if ( $false -eq (New-Object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) ) { | |
Write-Warning "Please run this as an administrator." | |
return | |
} | |
#region REAGENTC.EXE to PSCustomObject | |
$OutputObject = [ordered]@{} | |
(& "$($env:windir)\system32\reagentc.exe" /info) | Select-String -SimpleMatch ':' | Where-Object { $_ -notmatch '^Information:|^REAGENTC.EXE' } | ForEach-Object { | |
$ArrayLine = $_.Line -split ':' | ForEach-Object { (Get-Culture).TextInfo.ToTitleCase($_) } | |
$OutputObject += @{ | |
"$( (Get-Culture).TextInfo.ToTitleCase($ArrayLine[0].Trim()).Replace(' ','') -replace '\(BCD\)','' )" = $ArrayLine[1].Trim().Replace(' ','') | |
} | |
} | |
$OutputObject = [PSCustomObject]$OutputObject | |
$OutputObject | Add-Member -MemberType ScriptProperty -Name DiskNumber -Value { [int]([regex]::Match($this.WindowsRELocation, '\\Harddisk(?<DiskNumber>.+?(?=\\))').Groups['DiskNumber'].Value) } | |
$OutputObject | Add-Member -MemberType ScriptProperty -Name PartitionNumber -Value { [int]([regex]::Match($OutputObject.WindowsRELocation, '\\Partition(?<PartitionNumber>.+?(?=\\))').Groups['PartitionNumber'].Value) } | |
#endregion | |
$OutputObject | Add-Member -MemberType NoteProperty -Name SharedSystemDisk -Value $null | |
$OutputObject | Add-Member -MemberType NoteProperty -Name SharedSystemPartition -Value $null | |
$OutputObject | Add-Member -MemberType NoteProperty -Name WindowsREPartitionBeforeSystemPartition -Value $null | |
$OutputObject | Add-Member -MemberType NoteProperty -Name WindowsREPartitionAfterSystemPartition -Value $null | |
$OutputObject | Add-Member -MemberType NoteProperty -Name SizeMB -Value $null | |
$OutputObject | Add-Member -MemberType NoteProperty -Name FreeSpaceMB -Value $null | |
if ( $OutputObject.WindowsREStatus -eq 'Enabled' ) { | |
$SystemDriveLetter = $env:SystemDrive -replace ':','' | |
$SystemPartition = Get-Partition -DriveLetter $SystemDriveLetter | |
Write-Verbose "SystemDiskNumber: $($SystemPartition.DiskNumber)" | |
Write-Verbose "SystemPartitionNumber: $($SystemPartition.PartitionNumber)" | |
Write-Verbose "SystemAccessPath: $( $SystemPartition.AccessPaths | Where-Object { $_ -match '\\Volume\{' } )" | |
Write-Verbose "WindowsRELocation: $($OutputObject.WindowsRELocation)" | |
Write-Verbose "WindowsREDiskNumber: $($OutputObject.DiskNumber)" | |
Write-Verbose "WindowsREPartitionNumber: $($OutputObject.PartitionNumber)" | |
$WindowsREPartition = Get-Partition -DiskNumber $OutputObject.DiskNumber -PartitionNumber $OutputObject.PartitionNumber | |
$OutputObject.SizeMB = [math]::Round($WindowsREPartition.Size / 1MB) | |
$WindowsREAccessPath = $WindowsREPartition.AccessPaths[0] | |
Write-Verbose "WindowsREAccessPath: $($WindowsREAccessPath)" | |
$OutputObject.FreeSpaceMB = [math]::Round((Get-WmiObject Win32_Volume | Where-Object { $_.DeviceId -eq $WindowsREAccessPath }).FreeSpace / 1MB) | |
$OutputObject.SharedSystemDisk = $SystemPartition.DiskNumber -eq $WindowsREPartition.DiskNumber | |
$OutputObject.SharedSystemPartition = $OutputObject.SharedSystemDisk -eq $true -and $SystemPartition.PartitionNumber -eq $WindowsREPartition.PartitionNumber | |
$OutputObject.WindowsREPartitionBeforeSystemPartition = $SystemPartition.DiskNumber -eq $WindowsREPartition.DiskNumber -and $WindowsREPartition.PartitionNumber -lt $SystemPartition.PartitionNumber | |
$OutputObject.WindowsREPartitionAfterSystemPartition = $SystemPartition.DiskNumber -eq $WindowsREPartition.DiskNumber -and $WindowsREPartition.PartitionNumber -gt $SystemPartition.PartitionNumber | |
} | |
Write-Host | |
switch ($true) { | |
( $OutputObject.WindowsREStatus -ne 'Enabled' ) { | |
Write-Warning "The recovery partition disabled." | |
return | |
} | |
( $OutputObject.WindowsREPartitionBeforeSystemPartition ) { | |
Write-Warning "The recovery partition is before system partition. Recovery partition upgrades may require more space than is currently available on the partition. Additional actions would be required if this occurs." | |
break | |
} | |
( $OutputObject.WindowsREPartitionAfterSystemPartition ) { | |
Write-host "The recovery partition is after system partition." -ForegroundColor Green | |
break | |
} | |
( $OutputObject.SharedSystemPartition ) { | |
Write-host "The recovery partition is shared with the system partition." -ForegroundColor Green | |
break | |
} | |
default { | |
Write-Host "Unexpected result." -ForegroundColor Red | |
} | |
} | |
$OutputObject | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment