You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 -Recurse to traverse all customer subdirectories, then filters for folders named like chain1, chain2 using 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/catch block 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:

  1. Update the $backupRoot variable to your actual network share path.
  2. Run the script in PowerShell (you'll need read permissions for the network share).
  3. Review the output to spot stale or inaccessible backups at a glance.

内容的提问来源于stack exchange,提问作者DBratty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:06:45