Newest backup file freshness powershell
Finds the newest file matching a pattern (e.g. *.bak) in a folder and reports its age in seconds. Common pattern for 'did last night's SQL backup actually run?'.
Placeholders only. Before running, replace
YOUR_URL with your capture endpoint's POST URL
.
(Open this page from your capture object to have these auto-filled.)
# backup-freshness.ps1 — age (s) of newest matching file in a folder.
$Url = "YOUR_URL/backup"
$Folder = "C:\Backups"
$Pattern = "*.bak"
$Newest = Get-ChildItem -LiteralPath $Folder -Filter $Pattern -File -ErrorAction SilentlyContinue `
| Sort-Object LastWriteTimeUtc -Descending | Select-Object -First 1
if ($Newest) {
$Age = [int]([DateTime]::UtcNow - $Newest.LastWriteTimeUtc).TotalSeconds
$Body = (@{
hostname = $env:COMPUTERNAME
folder = $Folder
pattern = $Pattern
newest_file = $Newest.Name
newest_size_mb = [math]::Round($Newest.Length / 1MB, 1)
newest_age_seconds = $Age
}) | ConvertTo-Json -Compress
} else {
$Body = (@{
hostname = $env:COMPUTERNAME; folder = $Folder; pattern = $Pattern
newest_file = $null; newest_age_seconds = -1
}) | ConvertTo-Json -Compress
}
Invoke-RestMethod -Uri $Url -Method Post -Body $Body `
-ContentType "application/json" -TimeoutSec 15 | Out-Null
Recommended pairing
Add a capture.value check to this capture object.
json_path = newest_age_seconds, op = >, threshold = 90000 (~25 hours, gives a daily backup some slack).
What is the filename?
backup-freshness.ps1 — this is the suggested name for the downloaded file. Rename freely if you prefer.
site1.erralert.com