Add time-out to System Restore Point creation #289

This commit is contained in:
Raphire
2025-08-16 01:37:43 +02:00
parent c881858f32
commit 62e95376a6

View File

@@ -777,16 +777,28 @@ function GetUserName {
function CreateSystemRestorePoint { function CreateSystemRestorePoint {
Write-Output "> Attempting to create a system restore point..." Write-Output "> Attempting to create a system restore point..."
$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"
if ($SysRestore.RPSessionInterval -eq 0) { if ($SysRestore.RPSessionInterval -eq 0) {
if ($Silent -or $( Read-Host -Prompt "System restore is disabled, would you like to enable it and create a restore point? (y/n)") -eq 'y') { if ($Silent -or $( Read-Host -Prompt "System restore is disabled, would you like to enable it and create a restore point? (y/n)") -eq 'y') {
try { $enableSystemRestoreJob = Start-Job {
Enable-ComputerRestore -Drive "$env:SystemDrive" try {
} catch { Enable-ComputerRestore -Drive "$env:SystemDrive"
Write-Host "Error: Failed to enable System Restore: $_" -ForegroundColor Red } catch {
Write-Host "Error: Failed to enable System Restore: $_" -ForegroundColor Red
Write-Output ""
return
}
}
$enableSystemRestoreJobDone = $enableSystemRestoreJob | Wait-Job -TimeOut 20
if (-not $enableSystemRestoreJobDone) {
Write-Host "Error: Failed to enable system restore and create restore point, operation timed out" -ForegroundColor Red
Write-Output "" Write-Output ""
Write-Output "Press any key to continue anyway..."
$null = [System.Console]::ReadKey()
return return
} }
} else { } else {
@@ -795,24 +807,35 @@ function CreateSystemRestorePoint {
} }
} }
# 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 {
Write-Host "Error: Unable to retrieve existing restore points: $_" -ForegroundColor Red
Write-Output ""
return
}
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) }
Write-Output "System restore point created successfully"
} catch { } catch {
Write-Host "Error: Unable to create restore point: $_" -ForegroundColor Red Write-Host "Error: Unable to retrieve existing restore points: $_" -ForegroundColor Red
Write-Output ""
return
} }
} else {
Write-Host "A recent restore point already exists, no new restore point was created." -ForegroundColor Yellow if ($recentRestorePoints.Count -eq 0) {
try {
Checkpoint-Computer -Description "Restore point created by Win11Debloat" -RestorePointType "MODIFY_SETTINGS"
Write-Output "System restore point created successfully"
} catch {
Write-Host "Error: Unable to create restore point: $_" -ForegroundColor Red
}
} else {
Write-Host "A recent restore point already exists, no new restore point was created." -ForegroundColor Yellow
}
}
$createRestorePointJobDone = $createRestorePointJob | Wait-Job -TimeOut 20
if (-not $createRestorePointJobDone) {
Write-Host "Error: Failed to create system restore point, operation timed out" -ForegroundColor Red
Write-Output ""
Write-Output "Press any key to continue anyway..."
$null = [System.Console]::ReadKey()
} }
Write-Output "" Write-Output ""