【问题标题】:Powershell - set up data removal with retentionPowershell - 设置保留的数据删除
【发布时间】:2020-04-08 11:56:52
【问题描述】:

我需要一个 PS 脚本来删除目录中的所有子文件夹,但以下内容除外:

  • 过去 7 天的所有内容
  • 上个月每周更新一个文件夹
  • 去年每个月的一个最新文件夹

我对 PS 还很陌生,已经花了很长时间挣扎。讨厌哭求救,但我想,这是正确的时刻。

谢谢!

【问题讨论】:

  • 添加到问题:什么版本的powershell? “过去 7 天的所有内容”的路径是什么?
  • 稍微编辑了问题。每个子文件夹都包含一个我要备份的数据单元。我需要将子文件夹保留在上述保留中并删除其他子文件夹。 powershell v.5

标签: powershell rotation backup retention


【解决方案1】:

现在完成(:

# Initiate the intervals in days by which the folders will be kept
$savedays = @('7', '14', '21', '30', '60', '90', '120', '150', '180', '210', '240', '270', '300', '330', '360')

# initiate array of folders to be deleted
$killlist = @()

# Start a for loop until hitting the one before last element of $savedays
For ($i=0;$i -lt ($savedays.Length -1 ); $i++) {

    # Get list of folders in the $path_main
    $killlist += @(Get-ChildItem $path_main |
        # Newest first
        Sort-Object -Property LastWriteTime -Descending | 
        # Between one and next elevement of #savedays, i.e. between 7 days ago and 14 days ago
        Where-Object { $_.LastWriteTime -lt (get-date).AddDays(-$savedays[$i]) -and $_.LastWriteTime -gt (get-date).AddDays(-$savedays[$i+1])} |
        # Exclude the most recent folder from the aforementioned period
        Select-Object -Skip 1
        ) 

}

# delete folders
foreach ($folder in $killlist) {
    # delete the folder including contents
    Remove-Item $folder.fullname –recurse
}

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多