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

PowerShell脚本开发需求:统计服务器缺失T02文件并导出CSV

问题

服务器中每个站点每日需生成24个T02格式文件,站点停机时对应文件会缺失。当前有400+站点,文件存储路径示例为Y:\Alloy\DEHRADUN\2022\01(Alloy文件夹下为站点文件夹,后续依次是年份、月份目录)。

现有PowerShell脚本仅能导出已存在的T02文件信息至CSV,但无法标记缺失文件:

$targDir = "Y:\Alloy\DEHRADUN\";
Get-ChildItem -Path $sub –Recurse *.T02 | Where { ! $_.PSIsContainer } | Select FullName,Name,LastWriteTime,Length| Export-Csv "C:\Reports\YearWise\Dehradun2022.csv" -NoTypeInformation

需要修改脚本,让生成的CSV同时显示缺失文件,并标记为“no file”。

解决方案

以下是修改后的脚本,会生成包含所有预期文件(存在/缺失)的CSV报告:

# 配置基础参数
$siteRootDir = "Y:\Alloy\DEHRADUN"
$outputCsvPath = "C:\Reports\YearWise\Dehradun2022.csv"

# 先收集所有已存在的T02文件,用日期+小时作为键存入字典,方便快速查询
$existingFiles = @{}
Get-ChildItem -Path $siteRootDir -Recurse -Filter "*.T02" -File | ForEach-Object {
    # 这里需要根据你的实际文件名格式调整正则,示例匹配类似 "DEHRADUN_20220101_00.T02" 的文件名
    if ($_.Name -match '(\d{8})_(\d{2})') {
        $datePart = $matches[1] # 提取YYYYMMDD格式的日期
        $hourPart = $matches[2] # 提取HH格式的小时
        $uniqueKey = "$datePart-$hourPart"
        $existingFiles[$uniqueKey] = $_
    }
}

# 自动遍历站点下所有年份、月份目录,生成需要检查的日期范围
$targetDates = Get-ChildItem -Path $siteRootDir -Directory | ForEach-Object {
    $yearFolder = $_
    Get-ChildItem -Path $yearFolder.FullName -Directory | ForEach-Object {
        $monthFolder = $_
        $year = [int]$yearFolder.Name
        $month = [int]$monthFolder.Name
        # 获取当月的所有天数
        $totalDays = [DateTime]::DaysInMonth($year, $month)
        # 生成当月每一天的日期对象
        1..$totalDays | ForEach-Object {
            $day = $_ -as [string]
            if ($day.Length -eq 1) { $day = "0$day" }
            [DateTime]"$year-$month-$day"
        }
    }
}

# 生成所有预期的文件条目(每天24个时段)
$reportItems = @()
foreach ($date in $targetDates) {
    $dateString = $date.ToString("yyyyMMdd")
    # 遍历00到23点的24个时段
    for ($hour = 0; $hour -lt 24; $hour++) {
        $hourString = $hour.ToString("00")
        $lookupKey = "$dateString-$hourString"
        # 构造预期的文件名和路径,需和实际命名规则一致
        $expectedFileName = "DEHRADUN_${dateString}_${hourString}.T02"
        $expectedFullPath = Join-Path -Path (Join-Path $siteRootDir $date.Year) (Join-Path $date.ToString("MM") $expectedFileName)

        if ($existingFiles.ContainsKey($lookupKey)) {
            # 文件存在,填充实际文件信息
            $file = $existingFiles[$lookupKey]
            $reportItems += [PSCustomObject]@{
                FullName       = $file.FullName
                Name           = $file.Name
                LastWriteTime  = $file.LastWriteTime
                Length         = $file.Length
                Status         = "exists"
            }
        } else {
            # 文件缺失,填充预期信息并标记状态
            $reportItems += [PSCustomObject]@{
                FullName       = $expectedFullPath
                Name           = $expectedFileName
                LastWriteTime  = $null
                Length         = $null
                Status         = "no file"
            }
        }
    }
}

# 导出最终报告到CSV
$reportItems | Export-Csv -Path $outputCsvPath -NoTypeInformation
注意事项
  1. 文件名格式适配:脚本默认假设文件名包含YYYYMMDD_HH格式的日期和小时段,如果你的实际文件名规则不同(比如日期格式、分隔符不一样),需要修改正则表达式'(\d{8})_(\d{2})'以及预期文件名的构造代码。
  2. 批量站点处理:如果要处理400+站点,可以在脚本外层加入对Y:\Alloy下所有站点文件夹的遍历循环,为每个站点生成单独的CSV报告。
  3. 性能优化:对于大量文件和站点,建议分批次处理,避免内存占用过高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 19:34:58