Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 调试器跳过花括号_Powershell_Debugging_Get Childitem - Fatal编程技术网

Powershell 调试器跳过花括号

Powershell 调试器跳过花括号,powershell,debugging,get-childitem,Powershell,Debugging,Get Childitem,我正在编写一个脚本来遍历文件夹的内容,检查每个子目录的日期是否早于上周六,然后删除比上周六早的文件夹,但由于某些原因,powershell调试器在Get ChildItem花括号中缺少我的断点。没有错误消息,但我需要在if语句中添加一些内容来删除文件夹。调试器从Get ChildItem{}的左括号跳到函数的末尾{ 这是我的代码: #do weekly cleanup of DisasterBackup folder function WeeklyCleanup($folderWeeklyCle

我正在编写一个脚本来遍历文件夹的内容,检查每个子目录的日期是否早于上周六,然后删除比上周六早的文件夹,但由于某些原因,powershell调试器在Get ChildItem花括号中缺少我的断点。没有错误消息,但我需要在if语句中添加一些内容来删除文件夹。调试器从Get ChildItem{}的左括号跳到函数的末尾{

这是我的代码:

#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
}
我给函数的目录如下所示:

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

我将其存储为字符串,并将其交给如下函数:

$folderToCleanupDatedSubdirs = "E:\Bak_TestDatedFolderCleanup"
WeeklyCleanup $folderToCleanupDatedSubdirs
它有一个很长的列表,其中可能有10-20个文件夹,其中一些文件夹的名称中有日期,如下所示:

$folderToCleanupDatedSubdirs = "E:\Bak_TestDatedFolderCleanup"
WeeklyCleanup $folderToCleanupDatedSubdirs
托洛克罗博大学2019-01-07

一旦我的脚本完成,它将删除所有日期早于上周六日期的子目录,但仅限于本月。我希望无论我在哪一天运行脚本,它都能正常工作

我从这个链接和其他链接中得到了我的想法:

这可能是Get ChildItem中的格式问题,但我看不到。我只关心传递给WeeklyCleanup函数的文件夹中的子文件夹。这些子文件夹中有文件夹,但我不想查看它们。我以前在dir参数中使用过此格式,所以我认为它不会转义任何不应该转义的内容。

orEach是一个脚本块,它有两个脚本块,第一个是隐式的-Begin类型。 也可以用括号括起来并附加.FullName

将属性扩展为字符串-它不再是对象,并且已丢失.LastWriteTime属性

为什么要将日期格式化为字符串?那么它是一个字符串,不再是日期

这里有一个更简单的变体:

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