根据自定义日期过滤器删除旧文件

时间:2017-08-08 16:09:07

标签: powershell

保留策略是"保留最后7个备份(过去一周),然后将星期五备份保留3个月。然后保持每个月的上周五备份一年"。

以下是涵盖两个保留条件的脚本,但无法完成第三个条件,即"保持每个月的上周五备份一年"。需要帮助制作IF块。

编辑:在不同的文件夹中移动文件然后在单独的文件夹上应用单独的保留脚本的解决方案是不可能的,因为里面有很多子文件夹。由于这是备份文件的全部内容,因此移动文件将使我们的依赖还原过程更加复杂,否则代码将需要在所有服务器上进行调整。

#----- define folder where files are located ----#
$TargetFolder = "C:\Users\chsa\Desktop\Retention"

#----- define extension ----#
$Extension = "*.txt"

#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)

#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse 

foreach ($File in $Files) 
{
  if ($File -ne $NULL -and $File.LastWriteTime -lt (Get-Date).AddYears(-1))
  {
    Remove-Item $File.FullName | Out-Null
    Write-Host $File
  }

  if ($File -ne $NULL -and $File.LastWriteTime -ge (Get-Date).AddMonths(-3) -and $File.LastWriteTime -lt (Get-Date).AddDays(-7) -and $File.LastWriteTime.DayOfWeek -ne "Friday")
  {
    Remove-Item $File.FullName | Out-Null
    Write-Host $File
  }
}

2 个答案:

答案 0 :(得分:0)

会添加评论,但我没有它的声誉。

首先,我定义了一个函数来获取“月末的最后一个星期五”,这似乎不可能在没有乱七八糟的混乱的情况下在一行上完成。

function LastFridayOfMonth([DateTime] $DateIn) 
{
    $numDaysArr = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    return ($numDaysArr[$DateIn.Month] - $DateIn.Day) -lt 7 -and $DateIn.DayOfWeek -eq [System.DayOfWeek]::Friday
}

然后,我更容易用逻辑反转来思考这个问题,DeMorgans定理可能会简化我的if语句中的逻辑,但它应该有效。

if (-not ($file.LastWriteTime -lt (Get-Date).AddYears(-1) -and $file.LastWriteTime -gt (Get-Date).AddMonths(-3) -and (LastFridayOfMonth $file.LastWriteTime)))
{
    Remove-Item $File.FullName | Out-Null
    Write-Host $File
}

注意,它没有考虑闰年,我只测试了LastFridayOfMonth函数,而不是if语句逻辑。在你开始删除文件之前检查一下...虽然你有一般的想法。

答案 1 :(得分:0)

下面的最终脚本。

解决方案并不理想,因为脚本使用多个循环,这听起来没必要,但是,要求似乎已经完成;并且脚本测试正常。

此外,该脚本还有一个附加条款,如果万一,Last-Friday-Of-Month的备份文件不可用,那么该脚本将保留倒数第二个星期五的备份文件。同样的方式,如果倒数第二也不可用,那么它将保持倒数第三;同样分别为第四周和第五周。非常感谢大家。

#----- calculate start and end of current month ----#
$startofmonth = Get-Date -day 1 -hour 0 -minute 0 -second 0
$endofmonth = (($startofmonth).AddMonths(1).AddSeconds(-1))

#----- define folder where files are located ----#
$TargetFolder = "C:\Users\chsa\Desktop\Retention"

#----- define extension ----#
$Extension = "*.txt"

#----- get files based on lastwrite filter and specified folder ---#

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -lt $startofmonth.AddMonths(-13) } 

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-13) -and $_.LastWriteTime -le $endofmonth.AddMonths(-13) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-13) -and $_.LastWriteTime -le $endofmonth.AddMonths(-13) -and $_.LastWriteTime.DayofWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-12) -and $_.LastWriteTime -le $endofmonth.AddMonths(-12) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-12) -and $_.LastWriteTime -le $endofmonth.AddMonths(-12) -and $_.LastWriteTime.DayofWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-11) -and $_.LastWriteTime -le $endofmonth.AddMonths(-11) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-11) -and $_.LastWriteTime -le $endofmonth.AddMonths(-11) -and $_.LastWriteTime.DayofWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-10) -and $_.LastWriteTime -le $endofmonth.AddMonths(-10) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-10) -and $_.LastWriteTime -le $endofmonth.AddMonths(-10) -and $_.LastWriteTime.DayofWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-9) -and $_.LastWriteTime -le $endofmonth.AddMonths(-9) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-9) -and $_.LastWriteTime -le $endofmonth.AddMonths(-9) -and $_.LastWriteTime.DayofWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-8) -and $_.LastWriteTime -le $endofmonth.AddMonths(-8) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-8) -and $_.LastWriteTime -le $endofmonth.AddMonths(-8) -and $_.LastWriteTime.DayofWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-7) -and $_.LastWriteTime -le $endofmonth.AddMonths(-7) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-7) -and $_.LastWriteTime -le $endofmonth.AddMonths(-7) -and $_.LastWriteTime.DayofWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-6) -and $_.LastWriteTime -le $endofmonth.AddMonths(-6) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-6) -and $_.LastWriteTime -le $endofmonth.AddMonths(-6) -and $_.LastWriteTime.DayofWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-5) -and $_.LastWriteTime -le $endofmonth.AddMonths(-5) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-5) -and $_.LastWriteTime -le $endofmonth.AddMonths(-5) -and $_.LastWriteTime.DayofWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-4) -and $_.LastWriteTime -le $endofmonth.AddMonths(-4) -and $_.LastWriteTime.DayofWeek -eq "Friday"} | Sort LastWriteTime -Desc | Select -Skip 1

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-4) -and $_.LastWriteTime -le $endofmonth.AddMonths(-4) -and $_.LastWriteTime.DayofWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-3) -and $_.LastWriteTime -le $endofmonth.AddMonths(-3) -and $_.LastWriteTime.DayOfWeek -ne "Friday"}


foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-2) -and $_.LastWriteTime -le $endofmonth.AddMonths(-2) -and $_.LastWriteTime.DayOfWeek -ne "Friday"}


foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth.AddMonths(-1) -and $_.LastWriteTime -le $endofmonth.AddMonths(-1) -and $_.LastWriteTime.DayOfWeek -ne "Friday"}


foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}

$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | where {$_.LastWriteTime -ge $startofmonth -and $_.LastWriteTime -lt (Get-Date).AddDays(-8) -and $_.LastWriteTime.DayOfWeek -ne "Friday"}

foreach ($File in $Files) 
{
   if ($File -ne $NULL)
   {
     Remove-Item $File.FullName | out-null
     #write-host $File
   }

}