PowerShell脚本开发需求:检测备份链文件夹近4天是否修改
PowerShell Script to Monitor Backup Chain Folder Modification Dates
Got it, let's put together a reliable PowerShell script that automates checking your network-shared backup chains and reports their status clearly. Here's a solution tailored to your needs:
The Script
# Define the root path of your backups (update this to match your network share) $backupRoot = "\\server\backups" # Calculate the cutoff date (4 days prior to today) $fourDaysAgo = (Get-Date).AddDays(-4) # Fetch all "chain" folders under each customer directory $chainFolders = Get-ChildItem -Path $backupRoot -Directory -Recurse | Where-Object { $_.Name -match "^chain\d+$" } # Initialize an array to store our status results $results = @() foreach ($folder in $chainFolders) { try { # Get the folder's last modified timestamp $lastModified = $folder.LastWriteTime # Determine if the backup is current or stale if ($lastModified -ge $fourDaysAgo) { $status = "✅ Backup is current (last modified: $($lastModified.ToString('yyyy-MM-dd HH:mm')))" } else { $status = "⚠️ Backup is STALE (last modified: $($lastModified.ToString('yyyy-MM-dd HH:mm')) - older than 4 days)" } # Add the result to our collection $results += [PSCustomObject]@{ FolderPath = $folder.FullName Status = $status } } catch { # Handle cases where we can't access the folder (permissions, network issues) $results += [PSCustomObject]@{ FolderPath = $folder.FullName Status = "❌ Failed to access folder: $($_.Exception.Message)" } } } # Display results in a clean, readable table $results | Format-Table -AutoSize # Optional: Export results to a CSV file for archiving or sharing # $results | Export-Csv -Path "C:\BackupStatus_$(Get-Date -Format 'yyyyMMdd').csv" -NoTypeInformation
Key Details & Usage Tips
- Targeted Scanning: The script uses
Get-ChildItem -Recurseto traverse all customer subdirectories, then filters for folders named likechain1,chain2using a regex match (^chain\d+$). Adjust this regex if your chain folder naming follows a different pattern. - Date Logic:
(Get-Date).AddDays(-4)sets the cutoff—any folder modified on or after this date is marked as current. - Error Handling: The
try/catchblock catches permission errors or inaccessible network paths, ensuring you don't miss problematic folders in your report. - Flexible Output: The default table view is great for quick checks, and the optional CSV export lets you save or share the report easily (uncomment that line and adjust the path as needed).
To use this:
- Update the
$backupRootvariable to your actual network share path. - Run the script in PowerShell (you'll need read permissions for the network share).
- Review the output to spot stale or inaccessible backups at a glance.
内容的提问来源于stack exchange,提问作者DBratty
相关产品推荐
相关产品推荐

