this post was submitted on 23 Jan 2024
1 points (100.0% liked)

Sysadmin

12 readers
1 users here now

A reddit dedicated to the profession of Computer System Administration.

founded 2 years ago
MODERATORS
 
This is an automated archive.

The original was posted on /r/sysadmin by /u/InternetStranger4You on 2024-01-23 20:46:30+00:00.


Turns out I have a lot of computers that will not install KB5028997 and fail with 0x80070643 - ERROR_INSTALL_FAILURE. I wrote a PowerShell script to find the recovery partition, disable WinRE, resize the OS partition -250MB, recreate the recovery partition based on if it's GPT or MBR, then reenable WinRE. This is coded/tested for single disk systems with normal partition layouts but should adapt if it's not regular. Here is the Microsoft support article I used to build this:

I tested it on a few machines that have GPT partitions, and it works great. I did simulate an MBR disk on my computer and the logic works but has not been tested on real computers. As always, test in your own environment. Not responsible for any damages.

#Script to fix the recovery partition for KB5028997 by /u/InternetStranger4You 
#Mostly Powershell version of Microsoft's support article: https://support.microsoft.com/en-us/topic/kb5028997-instructions-to-manually-resize-your-partition-to-install-the-winre-update-400faa27-9343-461c-ada9-24c8229763bf    
#Test in your own environment before running. Not responsible for any damages.

#Run reagentc.exe /info and save the output
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "reagentc.exe"
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = '/info'
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()

#Disable Windows recovery environment
Start-Process "reagentc.exe" -ArgumentList "/disable" -Wait -NoNewWindow

#Verify that disk and partition are listed in reagentc.exe /info. If blank, then something is wrong with WinRE
if(($stdout.IndexOf("harddisk") -ne -1) -and ($stdout.IndexOf("partition") -ne -1)){
    #Get recovery disk number and partition number
    $DiskNum=$stdout.substring($stdout.IndexOf("harddisk")+8,1)
    $RecPartNum=$stdout.substring($stdout.IndexOf("partition")+9,1)

    #Resize OS partition
    $size=Get-Disk $DiskNum | Get-Partition -PartitionNumber ($RecPartNum-1) |Select-Object -ExpandProperty Size
    Get-Disk $DiskNum | Resize-Partition -PartitionNumber ($RecPartNum-1) -Size ($size - 250MB)

    #Remove the recovery partition
    Get-Disk $DiskNum | Remove-Partition -PartitionNumber $RecPartNum -Confirm:$false

    #Create new partion with diskpart script
    $DiskpartScriptPath = $env:TEMP
    $DiskpartScriptName = "ResizeREScript.txt"
    $DiskpartScript = $DiskpartScriptPath+'\'+$DiskpartScriptName
    "sel disk $($DiskNum)"|Out-File -FilePath $DiskpartScript -Encoding utf8 -Force
    $PartStyle = Get-Disk $DiskNum |Select-Object -ExpandProperty PartitionStyle
    if($PartStyle -eq "GPT"){
        #GPT partition commands
        "create partition primary id=de94bba4-06d1-4d40-a16a-bfd50179d6ac"|Out-File -FilePath $DiskpartScript -Encoding utf8 -Append -Force
        "gpt attributes =0x8000000000000001"|Out-File -FilePath $DiskpartScript -Encoding utf8 -Append -Force
    }else{
        #MBR partition command
        "create partition primary id=27"|Out-File -FilePath $DiskpartScript -Encoding utf8 -Append -Force
    }
    "format quick fs=ntfs label=`"Windows RE tools`""|Out-File -FilePath $DiskpartScript -Encoding utf8 -Append -Force
    Start-Process "diskpart.exe" -ArgumentList "/s $($DiskpartScriptName)" -Wait -NoNewWindow -WorkingDirectory $DiskpartScriptPath

    #Enable the recovery environment
    Start-Process "reagentc.exe" -ArgumentList "/enable" -Wait -NoNewWindow

}else{
    Write-Warning "Recovery partition not found. Aborting script."
}

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here