Windows Server File Share Backup Audit with PowerShell and Stale Job Detection
A practical build for a PowerShell script that audits file share backups on a Windows Server and detects stale backup jobs.
Expected Outcome
A functional PowerShell script that audits file share backups and identifies stale jobs, providing a report of the findings.
Assumptions
- Windows Server with PowerShell installed
- Administrative access to the server
- Basic knowledge of PowerShell scripting
Bill of Materials
- Windows Server
- PowerShell ISE or any text editor for scripting
- Access to file share locations
Build Steps
- Set Up PowerShell Environment
Open PowerShell ISE or your preferred text editor to create a new script.
Changes system state: review before running
New-Item -Path 'C:\Scripts' -ItemType Directory -Force Set-Location -Path 'C:\Scripts'
- Define Variables
Create variables for file share paths and backup job parameters.
Safe to run: read-only
$fileShares = 'C:\FileShare1', 'C:\FileShare2' $backupJobThreshold = (Get-Date).AddDays(-30)
- Audit File Shares
Loop through each file share to audit backup jobs. Manual action: $backupJobs | Select-Object Name, LastWriteTime | Format-Table.
Safe to run: read-only
foreach ($share in $fileShares) { $backupJobs = Get-ChildItem -Path $share -Recurse | Where-Object { $_.LastWriteTime -lt $backupJobThreshold } if ($backupJobs) { Write-Output 'Stale backup jobs found in: ' + $share } else { Write-Output 'No stale backup jobs in: ' + $share } } - Log Results
Log the results of the audit to a text file for future reference.
Changes system state: review before running
$logFile = 'C:\Scripts\BackupAuditLog.txt' foreach ($share in $fileShares) { $backupJobs = Get-ChildItem -Path $share -Recurse | Where-Object { $_.LastWriteTime -lt $backupJobThreshold } if ($backupJobs) { Add-Content -Path $logFile -Value ('Stale backup jobs found in: ' + $share) $backupJobs | ForEach-Object { Add-Content -Path $logFile -Value ('Job: ' + $_.Name + ', Last Run: ' + $_.LastWriteTime) } } else { Add-Content -Path $logFile -Value ('No stale backup jobs in: ' + $share) } } - Schedule the Script
Use Task Scheduler to run the script at regular intervals.
Changes system state: review before running
schtasks /create /tn 'BackupAudit' /tr 'powershell.exe -File C:\Scripts\YourScriptName.ps1' /sc daily /st 02:00
Validation
- Check the BackupAuditLog.txt file for entries.
- Verify that the script runs without errors in PowerShell.
- Confirm that stale jobs are correctly identified.
Troubleshooting
- If a step fails, capture the exact command, exit code, and log line before retrying or changing the design.
- Check task history, dataset permissions, snapshot names, and available capacity before rerunning backup or replication jobs.
Cleanup or Rollback
- Keep a copy of working configuration, compose files, scripts, and service credentials before removing containers, packages, or data directories.
- Do not delete snapshots, datasets, backup jobs, or replication targets until a restore test has succeeded.
- Rollback by restoring the prior snapshot or backup job configuration from the saved export.
Next Improvements
- Review the audit log regularly.
- Modify the script to include email notifications for stale jobs.
- Consider implementing automated cleanup of stale jobs.