【问题标题】:Powershell Script to clean up logs on a Monthly Basis每月清理日志的 Powershell 脚本
【发布时间】:2021-01-06 22:51:21
【问题描述】:

Powershell 新手又来了一个问题。

我们目前有一个存储文本文件的日志文件夹。我的主管希望我创建一个只有当月日志可见的脚本。之前的所有月份都应移至存档文件夹。他希望我创建一个我们可以每月运行一次的 powershell 脚本来实现这一目标。

例如,我们的日志文件夹应该只包含 2021 年 1 月的日志。任何早于 2021 年 1 月的日志都应该存档。一旦 2021 年 2 月 1 日到来,2021 年 1 月的所有日志都应移至存档文件夹,依此类推。

如何实现查看文件夹并仅保留当月日志的脚本?非常感谢任何指导、资源、视频等!我一直在互联网上搜索资源,但我还没有找到适合我需要的任何东西。

更新:能够在这里找到一个精彩的脚本:PowerShell: Sort and Move Files by Date to month and year,由 Thomas Maurer 提供(全归功于他!)

# Get the files which should be moved, without folders
$files = Get-ChildItem 'C:\Users\testuser\Desktop\log' -Recurse | where {!$_.PsIsContainer}
 
# List Files which will be moved
$files
 
# Target Filder where files should be moved to. The script will automatically create a folder for the year and month.
$targetPath = 'C:\Users\testuser\Desktop\log\archive'
 
foreach ($file in $files)
{
# Get year and Month of the file
# I used LastWriteTime since this are synced files and the creation day will be the date when it was synced
$year = $file.LastWriteTime.Year.ToString()
$month = $file.LastWriteTime.Month.ToString()
 
# Out FileName, year and month
$file.Name
$year
$month
 
# Set Directory Path
$Directory = $targetPath + "\" + $year + "\" + $month
# Create directory if it doesn't exsist
if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}
 
# Move File to new location
$file | Move-Item -Destination $Directory
}

我现在想要实现的目标:这个脚本效果很好,但我正在尝试修改它,以便可以移动除当前月份之外的所有内容。我仍在研究和调查,所以如果我能够为我找出这个缺失的部分,我会确保更新我的帖子。谢谢大家的帮助!

【问题讨论】:

  • 通常我会说这个问题太宽泛了,你应该把问题分解成更具体的问题,包括你尝试过的细节等,但几个月前我写了一个脚本基本上就是这样做的。 Archiver on PowerShell Gallery,如果你想制作自己的版本,source is available with an MIT license
  • 至少有数千个脚本可以执行您所描述的操作 - 并且许多都可以在 inet 上找到。 [grin i>] 你搜索过这样的吗?什么没有按预期工作?你遇到了什么错误?
  • 感谢@zzzzBov 提供的链接!我会看看它们,因为我认为它可能会在不包括当月的情况下缺少我的部分:)我很感激这些资源!
  • 嗨@Lee_Dailey - 你是绝对正确的,我承认我在有点恐慌的情况下发布了这个,因为我仍在寻找可能的解决方案。我已经用我能够找到的由 Thomas Maurer 创建的脚本更新了我的帖子,这对我的问题有所帮助:) 目前,我正试图弄清楚如何排除当前月份。我正在查看之前评论者提供的资源,希望我能将其拼凑起来。
  • @CPG - 要过滤掉当前月份的文件,您需要决定如何定义“当前月份”。 [grin] 一种方法是使用相关日期时间对象的.Month 属性。另一种方法是使用 .AddDays(-30) 获取 30 天前的日期,然后将其用作过滤器。

标签: powershell archive


【解决方案1】:

保留本月最后修改的文件的一种方法是使用一个小的帮助函数,将 LastWriteTime 日期格式化为字符串 yyyy\MM

function Format-YearMonth ([datetime]$date) {
    # simply output a string like "2021\01"
    return '{0:yyyy\\MM}' -f $date
}

$sourcePath = 'C:\Users\testuser\Desktop\log'
$targetPath = 'C:\Users\testuser\Desktop\log\archive'
$thisMonth  = Format-YearMonth (Get-Date)

# Get the files which should be moved, without folders 
# this can be more efficient if all files have the same extension on which
# you can use -Filter '*.log' for instance.
Get-ChildItem -Path $sourcePath -File -Recurse | 
    # filter out the files that have a LastWriteTime for this year and month
    Where-Object {(Format-YearMonth $_.LastWriteTime) -ne $thisMonth } |
    ForEach-Object {
        # Set destination Path
        $Directory = Join-Path -Path $targetPath -ChildPath (Format-YearMonth $_.LastWriteTime)
        # Create directory if it doesn't exsist
        if (!(Test-Path $Directory)) {
            $null = New-Item $Directory -type Directory
        }
        Write-Host "Moving file '$($_.FullName)' to '$Directory'"
        # Move File to new location
        $_ | Move-Item -Destination $Directory -Force
    }

【讨论】:

    猜你喜欢
    • 2019-08-13
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2022-08-22
    • 2013-09-06
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多