首页 > 解决方案 > 用于报告多个共享上的旧文件夹的 Powershell 脚本

问题描述

我希望得到一些指导。我需要分析 6 个月内未修改的文件夹的多个共享。对于每个共享,我定义了需要检查的特定路径。应为每个共享生成一个 csv。powershell 脚本可以很好地检查单个共享,但我无法让它对多个共享正常工作。这是脚本的示例。

$age = (Get-Date).AddMonths(-6)

# Export path
$outputPath = "D:\Scripting\xPowershell_Results\"

# Export file name
$FilePath1 = "OldMediaPath1.csv"
$FilePath2 = "OldMediaPath2.csv"

# Paths to scan
$CheckPath1 = @(
"\\servername\share1\Projects"
"\\servername\share1\Restored"
 )

 $CheckPath2 = @(
"\\servername\share2\Graphics"
 )


$folders = dir $CheckPath1 | where {$_.PSiscontainer -eq $true} | Where-Object {($_.LastWriteTime -lt $age)} 
$results = foreach ($folder in $folders) {
    $bytecount = (dir $folder.fullname -recurse | Measure-Object -property length -sum).sum
    switch ($bytecount)
    {
        {$_ -lt 1KB} { $size = "{0:N0} Bytes" -f $_; break }
        {$_ -lt 1MB} { $size = "{0:N2} KB" -f ($_ / 1KB); break }
        {$_ -lt 1GB} { $size = "{0:N2} MB" -f ($_ / 1MB); break }
        {$_ -lt 1TB} { $size = "{0:N2} GB" -f ($_ / 1GB); break } 
        {$_ -lt 1PB} { $size = "{0:N2} TB" -f ($_ / 1TB); break } 
        default { $size = "{0:N2} PB" -f ($_ / 1PB) }
 
    }
    [PSCustomObject]@{
        Name = $folder.fullname
        Size = $size
        LastWriteTime = $folder.LastWriteTime 
    }
}
$results | Export-Csv $outputPath$FilePath1 -NoTypeInformation

标签: arrayspowershellmultidimensional-arrayforeachfilepath

解决方案


推荐阅读