
나는 특정 파일 유형별로 드라이브 목록을 작성하는 스크립트를 작성해 왔습니다. (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
[...]