I have been working through a script that needs to parse about 2 TB of data, locate any “archive” folders, then look at the objects recursively inside those “archive” folders for old stuff. If any objects are found; log the absolute path, then delete.
I have a working script that can find all the “archive” folders, but am struggling with transitioning over to getting the child items under the archive folder.
This is what I have –
$TopLevelPaths = @( "Local-shares" , "Net-shares" , "Net-Users" , "Temp" ) $DaysBack = "-400" $CSVPath = "C:\Scripts\" $ErrorDataPath = "C:\Scripts\ErrorData.txt" $CurrentDate = Get-Date $DateToDelete = $CurrentDate.AddDays($DaysBack) $MyFilter = "*archiv*" $PathData = @() $OldDataFile = $CSVPath + "OldData.csv" $ArchiveLocations = @() #// Find any flavor of "Archive" named folder and add to array ForEach ($TopLevelPath in $TopLevelPaths) { $CSVFile = $CSVPath + $TopLevelPath + "_Script_Output.csv" $tempTopLevelPath = "D:\" + $TopLevelPath $PathData += Get-ChildItem $tempTopLevelPath -Recurse -Force -ErrorAction SilentlyContinue -ErrorVariable ErrorData | Where-Object {$_.PSIsContainer -and $_.Name -like $MyFilter} | #Select-Object $_ | Select-Object @{l="Path";e={$_.PSPath.substring(38,$_.PSPath.Length-38)}} $PathData | Export-CSV -NoTypeInformation -delimiter '|' $CSVFile #// Works through Errors generated in Get-ChildItem command and drops them here $ErrorData.Exception >> $ErrorDataPath $ErrorData.TargetObject >> $ErrorDataPath } #// For Each entry in each CSV, Find Old Files ForEach ($TopLevelPath in $TopLevelPaths) { #// Build dynamic csv file names $tempCSV = $CSVPath + $TopLevelPath + "_Script_Output.csv" $ArchiveLocations = Import-CSV $tempCSV ForEach ($ArchiveLocation in $ArchiveLocations) { $OldFiles += Get-ChildItem $ArchiveLocation -Recurse -Force | Select-Object FullName, LastWriteTime, Length | Where-Object ($ArchiveLocation.LastWriteTime -lt $DateToDelete) Write-Host OldFiles $OldFiles.Length #$OldFiles | Export-CSV -NoTypeInformation -delimiter '|' $tempCSV } }
The error I’m getting back is
Get-ChildItem : Cannot find drive. A drive with the name '@{Path=D' does not exist. At C:\Scripts\test.ps1:38 char:17 + $OldFiles += Get-ChildItem $ArchiveLocation -Recurse -Force | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (@{Path=D:String) [Get-ChildItem], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand OldFiles 0
Where Line 38 is
$OldFiles += Get-ChildItem $ArchiveLocation -Recurse -Force |
and the OldFiles 0 is just the Write-Host command at the bottom of the last ForEach loop. I’m thinking (or optimistic) that I am close, but not sure. Any help to make the leap from the first process to the next will be appreciated.
A sample of the output CSV file ($PathData) is below.
"Path" "D:\Local-shares\Shares\Engineering\Archive" "D:\Local-shares\Shares\Engineering\Archives" "D:\Local-shares\Shares\Engineering\Archives\Manuals\archive"