检查远程计算机上的文件的时间戳早于 X 小时并将结果导出到 CSV

Check files on remote computers for time stamp older than X hours and export results to CSV

我们正在尝试 运行 针对一堆远程计算机的脚本来检查固定文件夹中超过 12 小时的文件的日期戳,并将结果 return CSV。日期范围需要灵活,因为它是昨天下午 6 点的设定时间,会随着时间的推移而变化。

$computers = Get-Content -Path computers.txt  
$filePath = "c:\temp\profile"
$numdays = 0
$numhours = 12
$nummins = 5   
function ShowOldFiles($filepath, $days, $hours, $mins)
{
    $files = $computers @(get-childitem $filepath -include *.* -recurse | where {($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)})
    if ($files -ne $NULL)
    {
        for ($idx = 0; $idx -lt $files.Length; $idx++)
        {
            $file = $files[$idx]
            write-host ("Old: " + $file.Name) -Fore Red
        }
    }
}

Write-output $computers, $numdays, $numhours, $nummins >> computerlist.txt

您可以 运行 在所有远程计算机上执行以下脚本:

$computers = Get-Content -Path computers.txt  
$logFile = "\ServerName\C$\Logfile.txt"

$date = "12/03/2002 12:00"
$limit = Get-Date $date

$computers | %{
    $filePath = "\$_\C$\temp\profile"

    $files = $null
    $files = Get-ChildItem -Path $filePath -Recurse -Force | `
                Where-Object {$_.CreationTime -lt $limit }

    If($files -ne $null){
        "-------------------------[$($_)]------------------------">> $logFile
        $files | Foreach {$_.FullName >> $logFile}
    }
}

这将检查给定文件夹 ($filePath) 中是否存在早于给定限制的文件。早于限制的文件将在给定的网络位置 $logFile.

中记录完整的文件路径

通过对@chard 早期代码的小改动,我设法获得了一个可行的解决方案。
输出日志文件仅 returns 早于代码中日期的文件。 这可以在 Excel 中使用其他输出来满足我们的需要。

我稍后会尝试上面更新的代码。

$computers = Get-Content -Path "C:\temp\computers.txt"
$logFile = "\SERVER\logs\output.txt"

$numdays = 3
$numhours = 10
$nummins = 5  

$limit = (Get-Date).AddDays(-$numdays).AddHours(-$numhours).AddMinutes(-$nummins)

$computers | %{
    $filePath = "\$_\C$\temp\profile\runtime.log"

    Get-ChildItem -Path $filePath -Recurse -Force | `
                Where-Object {$_.LastWriteTime -lt $limit } |  
                foreach {"$($_)">> $logFile}
}