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

@@ -782,6 +782,7 @@ function CreateSystemRestorePoint {
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') {
$enableSystemRestoreJob = Start-Job {
try { try {
Enable-ComputerRestore -Drive "$env:SystemDrive" Enable-ComputerRestore -Drive "$env:SystemDrive"
} catch { } catch {
@@ -789,12 +790,24 @@ function CreateSystemRestorePoint {
Write-Output "" Write-Output ""
return 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 "Press any key to continue anyway..."
$null = [System.Console]::ReadKey()
return
}
} else { } else {
Write-Output "" Write-Output ""
return return
} }
} }
$createRestorePointJob = Start-Job {
# Find existing restore points that are less than 24 hours old # Find existing restore points that are less than 24 hours old
try { try {
$recentRestorePoints = Get-ComputerRestorePoint | Where-Object { (Get-Date) - [System.Management.ManagementDateTimeConverter]::ToDateTime($_.CreationTime) -le (New-TimeSpan -Hours 24) } $recentRestorePoints = Get-ComputerRestorePoint | Where-Object { (Get-Date) - [System.Management.ManagementDateTimeConverter]::ToDateTime($_.CreationTime) -le (New-TimeSpan -Hours 24) }
@@ -814,6 +827,16 @@ function CreateSystemRestorePoint {
} else { } else {
Write-Host "A recent restore point already exists, no new restore point was created." -ForegroundColor Yellow 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 ""
} }