Prompt after failure to create restore point (#469)

This commit is contained in:
Jeffrey
2026-02-15 16:54:01 +01:00
committed by GitHub
parent 5dcc8bffdd
commit 687c089f2e

View File

@@ -3387,6 +3387,7 @@ function ExecuteAllChanges {
function CreateSystemRestorePoint { function CreateSystemRestorePoint {
$SysRestore = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" -Name "RPSessionInterval" $SysRestore = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" -Name "RPSessionInterval"
$failed = $false
if ($SysRestore.RPSessionInterval -eq 0) { if ($SysRestore.RPSessionInterval -eq 0) {
# In GUI mode, skip the prompt and just try to enable it # In GUI mode, skip the prompt and just try to enable it
@@ -3406,66 +3407,91 @@ function CreateSystemRestorePoint {
if (-not $enableSystemRestoreJobDone) { if (-not $enableSystemRestoreJobDone) {
Remove-Job -Job $enableSystemRestoreJob -Force -ErrorAction SilentlyContinue Remove-Job -Job $enableSystemRestoreJob -Force -ErrorAction SilentlyContinue
Write-ToConsole "Error: Failed to enable system restore and create restore point, operation timed out" -ForegroundColor Red Write-ToConsole "Error: Failed to enable system restore and create restore point, operation timed out" -ForegroundColor Red
return $failed = $true
} }
else { else {
$result = Receive-Job $enableSystemRestoreJob $result = Receive-Job $enableSystemRestoreJob
Remove-Job -Job $enableSystemRestoreJob -ErrorAction SilentlyContinue Remove-Job -Job $enableSystemRestoreJob -ErrorAction SilentlyContinue
if ($result) { if ($result) {
Write-ToConsole $result -ForegroundColor Red Write-ToConsole $result -ForegroundColor Red
return $failed = $true
} }
} }
} }
else { else {
Write-ToConsole "" Write-ToConsole ""
return $failed = $true
} }
} }
$createRestorePointJob = Start-Job { if (-not $failed) {
# Find existing restore points that are less than 24 hours old $createRestorePointJob = Start-Job {
try { # Find existing restore points that are less than 24 hours old
$recentRestorePoints = Get-ComputerRestorePoint | Where-Object { (Get-Date) - [System.Management.ManagementDateTimeConverter]::ToDateTime($_.CreationTime) -le (New-TimeSpan -Hours 24) }
}
catch {
return @{ Success = $false; Message = "Error: Unable to retrieve existing restore points: $_" }
}
if ($recentRestorePoints.Count -eq 0) {
try { try {
Checkpoint-Computer -Description "Restore point created by Win11Debloat" -RestorePointType "MODIFY_SETTINGS" $recentRestorePoints = Get-ComputerRestorePoint | Where-Object { (Get-Date) - [System.Management.ManagementDateTimeConverter]::ToDateTime($_.CreationTime) -le (New-TimeSpan -Hours 24) }
return @{ Success = $true; Message = "System restore point created successfully" }
} }
catch { catch {
return @{ Success = $false; Message = "Error: Unable to create restore point: $_" } return @{ Success = $false; Message = "Error: Unable to retrieve existing restore points: $_" }
} }
}
else {
return @{ Success = $true; Message = "A recent restore point already exists, no new restore point was created"; Warning = $true }
}
}
$createRestorePointJobDone = $createRestorePointJob | Wait-Job -TimeOut 20 if ($recentRestorePoints.Count -eq 0) {
try {
if (-not $createRestorePointJobDone) { Checkpoint-Computer -Description "Restore point created by Win11Debloat" -RestorePointType "MODIFY_SETTINGS"
Remove-Job -Job $createRestorePointJob -Force -ErrorAction SilentlyContinue return @{ Success = $true; Message = "System restore point created successfully" }
Write-ToConsole "Error: Failed to create system restore point, operation timed out" -ForegroundColor Red }
} catch {
else { return @{ Success = $false; Message = "Error: Unable to create restore point: $_" }
$result = Receive-Job $createRestorePointJob }
Remove-Job -Job $createRestorePointJob -ErrorAction SilentlyContinue
if ($result.Success) {
if ($result.Warning) {
Write-ToConsole $result.Message -ForegroundColor Yellow
} }
else { else {
Write-ToConsole $result.Message return @{ Success = $true; Message = "A recent restore point already exists, no new restore point was created" }
} }
} }
else {
Write-ToConsole $result.Message -ForegroundColor Red $createRestorePointJobDone = $createRestorePointJob | Wait-Job -TimeOut 20
if (-not $createRestorePointJobDone) {
Remove-Job -Job $createRestorePointJob -Force -ErrorAction SilentlyContinue
Write-ToConsole "Error: Failed to create system restore point, operation timed out" -ForegroundColor Red
$failed = $true
} }
else {
$result = Receive-Job $createRestorePointJob
Remove-Job -Job $createRestorePointJob -ErrorAction SilentlyContinue
if ($result.Success) {
Write-ToConsole $result.Message
}
else {
Write-ToConsole $result.Message -ForegroundColor Red
$failed = $true
}
}
}
# Ensure that the user is aware if creating a restore point failed, and give them the option to continue without a restore point or cancel the script
if ($failed) {
if ($script:GuiConsoleOutput) {
$result = [System.Windows.MessageBox]::Show(
'Failed to create a system restore point. Do you want to continue without a restore point?',
'Restore Point Creation Failed',
[System.Windows.MessageBoxButton]::YesNo,
[System.Windows.MessageBoxImage]::Warning
)
if ($result -ne [System.Windows.MessageBoxResult]::Yes) {
$script:CancelRequested = $true
return
}
}
elseif (-not $Silent) {
Write-ToConsole "Failed to create a system restore point. Do you want to continue without a restore point? (y/n)" -ForegroundColor Yellow
if ($( Read-Host ) -ne 'y') {
$script:CancelRequested = $true
return
}
}
Write-ToConsole "Warning: Continuing without restore point" -ForegroundColor Yellow
} }
} }