Сценарий powershell, удаляющий файлы с первого месяца месяца, вместо того, чтобы хранить их - PullRequest
0 голосов
/ 03 мая 2019
$RollingYear = (Get-Date).AddDays(-365)
$Rolling30Days = (Get-Date).AddDays(-30)

#Clean-Up Old Backup Files
Get-ChildItem 'D:\Server_Backup\' | ForEach-Object {
    if ( $_.LastWriteTime.AddDays(-365) -lt $RollingYear) {
        Write-Host "I want to remove files older than a year"
        Remove-Item
    } Elseif ( $_.LastWriteTime.AddDays(-30) -lt $Rolling30Days -and $_.LastWriteTime.Date -ne (Get-Date -Year $_.LastWriteTime.Year, -Month $_.LastWriteTime.Month -Day 1)) {
        Write-Host "I want to remove files older than 1 month, but not the first of the month"
        Remove-Item
    } Else {
        Write-Host 'Nothing to remove'
    }
}

Это должно сохранить файлы с первого числа месяца старше 30 дней и удалить остальные.В настоящее время он снимает 1-е число месяца и сохраняет остаток.Что я сделал не так?

1 Ответ

1 голос
/ 03 мая 2019

ваша логика смутила меня [ blush ], поэтому я переписал ее, используя количество дней вместо сравнения даты и времени.

$SourceDir = $env:TEMP

$Today = (Get-Date).Date
$YearInDays = 365
$MonthInDays = 30

#Clean-Up Old Backup Files
Get-ChildItem -LiteralPath $SourceDir -File |
    ForEach-Object {
        $DaysOld = ($Today - $_.LastWriteTime).Days
        if ($DaysOld -gt $YearInDays)
            {
            ''
            'LWT = {0}' -f $_.LastWriteTime
            'File age in days = {0}' -f $DaysOld
            Write-Host "I want to remove files older than a year"

            Remove-Item $_.FullName -WhatIf
            }
            Elseif ($DaysOld -gt $MonthInDays -and
                $_.LastWriteTime.Day -ne 1)
            {
            ''
            'LWT = {0}' -f $_.LastWriteTime
            'File age in days = {0}' -f $DaysOld
            Write-Host "I want to remove files older than 1 month, but not the first of the month"

            Remove-Item $_.FullName -WhatIf
            }
            Else
            {
            ''
            'LWT = {0}' -f $_.LastWriteTime
            'File age in days = {0}' -f $DaysOld
            Write-Host 'Nothing to remove'
            }
    }

вывод ...

LWT = 2015-11-03 6:54:02 PM
File age in days = 1276
I want to remove files older than a year
What if: Performing the operation "Remove File" on target "C:\Temp\FXSAPIDebugLogFile.txt".

LWT = 2019-03-04 12:45:30 PM
File age in days = 59
I want to remove files older than 1 month, but not the first of the month
What if: Performing the operation "Remove File" on target "C:\Temp\Itunes_Default-Rating_Set.ps1_2019-05-03.log".

LWT = 2019-03-01 12:44:23 PM
File age in days = 62
Nothing to remove

LWT = 2015-11-03 9:35:03 PM
File age in days = 1276
I want to remove files older than a year
What if: Performing the operation "Remove File" on target "C:\Temp\qtsingleapp-fmlast-93b-1-lockfile".

LWT = 2019-05-03 6:43:11 AM
File age in days = 0
Nothing to remove
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...