Search code examples
powershelldebuggingget-childitem

debugger jumping over curly brackets


I am writing a script to go through the contents of a folder, check to see if each subdir is dated before last Saturday, and then remove the folders that are older than last Saturday, but for some reason the powershell debugger is missing my breakpoints within the Get-ChildItem curly brackets. There isn't an error message, but I need to add something within the if statement to remove the folder. The debugger jumps to the end of the function from open curly bracket of Get-ChildItem{}{

This is my code:

#do weekly cleanup of DisasterBackup folder
function WeeklyCleanup($folderWeeklyCleanupDatedSubdirs) {
   #find out filename with Saturday date before current date
   (Get-ChildItem -Path $folderWeeklyCleanupDatedSubdirs -Filter -Directory).Fullname | ForEach {$_}     
   { #####debugger jumps from here to end bracket of WeeklyCleanup function when I step over
      write-output $_
      #check to see if item is before day we want to remove
      $lastSaturday = GetLastSaturdayDate
      if($_.LastWriteTime -le $lastSaturday)
      {
         #will remove dir once I've checked it's giving me the right ones
         Write-Output $_
         Write-Output " 1 "
      }

   }
} ############debugger skips to here

function GetLastSaturdayDate()
{
   $date = "$((Get-Date).ToString('yyyy-MM-dd'))"
   for($i=1; $i -le 7; $i++){
      if($date.AddDays(-$i).DayOfWeek -eq 'Saturday')
      {
         $date.AddDays(-$i)
         break
      }
   }
   return $date
}

The directory I'm giving to the function looks like this:

E:\Bak_TestDatedFolderCleanup

I store it as a string and give it to the function like this:

$folderToCleanupDatedSubdirs = "E:\Bak_TestDatedFolderCleanup"
WeeklyCleanup $folderToCleanupDatedSubdirs

and it has a long list of maybe 10-20 folders in it, some of which have a date, in the name, like this:

toLocRobo_2019-01-07

Once my script is done, it will have removed all subdirs that are dated prior to last Saturday's date, but only for the current month. I want this do work no matter what day I run the script.

I've been getting my ideas from this link and other ones: AddDays escape missing

It's probably a format issue within Get-ChildItem but I don't see it. I only care about the subdirs within the folder passed to the WeeklyCleanup function. There are folders within those subdirs, but I don't want them looked at. I've used this format before for my dir parameter so I don't think it's escaping anything it shouldn't.


Solution

  • Your ForEach is a ForEach-Object it has two scriptblocks, the first is implicitly of -Begin type.
    Also by enclosing in parentheses and appending the .FullName

    (Get-ChildItem -Path $folderWeeklyCleanupDatedSubdirs -Filter -Directory).Fullname
    

    you expand the property to a string - It's no more an object and has lost the .LastWriteTime property.

    Why do format the date ToString? It's a string then, no more a date.

    Here a more simple variant:

    function GetLastSaturdayDate(){
       $Date = Get-Date
       $Date.AddDays(-($Date.DayOfWeek+1)%7)} # on a saturday returns same date
      #$Date.AddDays(-($Date.DayOfWeek+1))}   # on a saturday returns previous sat.
    }
    
    function WeeklyCleanup($folderWeeklyCleanupDatedSubdirs) {
        Get-ChildItem -Path $folderWeeklyCleanupDatedSubdirs -Directory | ForEach {
            "Processing {0}" -f $_.FullName
            if($_.LastWriteTime -le (GetLastSaturdayDate)){
                "LastWriteTime {0:D}" -f $_.LastWriteTime
                # $_ | Remove-Item   -Force -Recurse # delete
            }
    
        }
    }
    
    $folderToCleanupDatedSubdirs = "E:\Bak_TestDatedFolderCleanup"
    WeeklyCleanup $folderToCleanupDatedSubdirs