I'm working on an automated cleanup script that uses Get-ChildItem
pretty liberally. I filter the contents to clean from an XML configuration with the -Include
flag since it takes a string array while -Filter
does not. Here's the offending block in question:
Function CleanFolders
{
Write-Log 'Cleaning Folders..' status
ForEach ($Folder in $Config.OS.$Build.Folders.Folder)
{
If (Test-Path $Folder.Name)
{
Try {
GCI $Folder.Name -Include $Folder.Include -Recurse -Force |
Where { ($_.Length -gt ([Int]$Folder.Threshold * 1MB)) -and
($_.LastWriteTime -lt (Get-Date).AddDays(-[Int]$Folder.Expiration)) } |
Sort $_.IsPSContainer -Descending |
% {
Try {
Write-Log "Deleting $($_.FullName)"
Remove-Item "$($_.FullName)" -Force
} Catch {
Write-Log 'Unable to delete item' error
Write-Log "[$($_.Exception.GetType().FullName)] - $($_.Exception.Message)" error
}
}
} Catch { Write-Log "[$($_.Exception.GetType().FullName)] - $($_.Exception.Message)" error }
} Else { Write-Log "$($Folder.Name) does not exist" error }
}
}
The script is run from a scheduled task as SYSTEM and for some reason, is not returning results from the -Include
flag that are not just a wildcard *
. Are there known issues with -Include
? This operation runs properly when run as a user account with admin privileges.