用於清點檔案類型的 Powershell 腳本

用於清點檔案類型的 Powershell 腳本

我一直在編寫一個腳本來按特定文件類型清點驅動器。 (AVI、MPG、MP3 等......)

我可以僅使用設定的驅動器和擴展名來使基本腳本正常工作,但我真的想讓它從文件中讀取擴展名,並從文件中讀取驅動器。

$dir = get-childitem z:\ –recurse
ForEach ($item in $dir)
{
If ($item.extension –eq '.avi')
    {
    $item | select-object length,fullname,LastWriteTime | Export-CSV  C:\temp\z-avi.csv –notypeinformation –append
    }
}

當我搜尋時,我只找到伺服器磁碟機空間腳本。

任何指導將不勝感激。

答案1

很混亂,但我使用 WMI 來獲取驅動器,然後根據獨特的擴充功能進行循環:

$computer = Get-ADcomputer ComputerName
$drives = Get-WmiObject win32_volume -ComputerName $computer.DNSHostName | Where-Object {$_.DriveType -eq 3 -and $_.DriveLetter -ne $null -and $_.Label -ne "System Reserved"}

Foreach ($drive in $drives)
{
    $allfiles = gci $drive.DriveLetter -recurse | Select Name,FullName,Extension,Length,LastWriteTime
    $extensions = $allfiles | Select -Unique Extension
    Foreach ($ext in $extensions)
    {
    $filename = ($drive | Select -ExpandProperty DriveLetter -First 1)[0] +     ($ext | Select -ExpandProperty Extension -First 1)
    $extensionfiles = $allfiles | Where-Object {$_.Extension -eq   $ext.extension}
    #$extensionfiles.count
    $extensionfiles | Export-Csv C:\Temp\$filename.csv -Notypeinformation
    }
}

WMI 呼叫將只帶回本機磁碟機。

答案2

像這樣的東西應該​​可以解決問題...請注意 Export-Csv 位元上的 -WhatIf。

在此範例中,所有 csv 檔案都將保存在 C:\temp 下。

$drives = Get-Content .\Drives.txt
$extensions = Get-Content .\Extensions.txt

foreach($drive in $drives) 
{
    $files = Get-ChildItem -Path "$drive`:\*" -Recurse -Include $($extensions | % { "*.$_" }) | where { $_.PSIsContainer -eq $false }
    $grouped = $files | Group-Object -Property Extension
    foreach ($group in $grouped)
    {
        $group | select -ExpandProperty Group | select Length, FullName, LastWriteTime | Export-Csv -Path "C:\Temp\$drive-$($group.Name.Replace('.','')).csv" -Append -NoTypeInformation -WhatIf
    }
}

其中 Drives.txt 每行有一個磁碟機號

C
D
E
[...]

Extensions.txt 每行都有一個副檔名。

mp3
mpg
avi
[...]

相關內容