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_Archive - Fatal编程技术网

每月清理日志的Powershell脚本

每月清理日志的Powershell脚本,powershell,archive,Powershell,Archive,Powershell新手又来了,还有一个问题 我们目前有一个日志文件夹,用于积累文本文件。我的主管希望我创建一个只有当月日志可见的脚本。以前的所有月份都应移到存档文件夹中。他想让我创建一个powershell脚本,我们可以每月运行一次来实现这一点 例如,我们的日志文件夹应该只有2021年1月的日志。任何早于2021年1月的文件都应存档。一旦2021年2月1日到达,2021年1月的所有日志都应该移动到归档文件夹中,依此类推 如何实现查看文件夹并仅保留当月日志的脚本?非常感谢任何指导、资源、视频等!

Powershell新手又来了,还有一个问题

我们目前有一个日志文件夹,用于积累文本文件。我的主管希望我创建一个只有当月日志可见的脚本。以前的所有月份都应移到存档文件夹中。他想让我创建一个powershell脚本,我们可以每月运行一次来实现这一点

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

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

更新:在这里找到了一个精彩的脚本:由Thomas Maurer提供(全归功于他!)


我现在想要实现的目标:这个脚本工作得很好,但我正在尝试修补它,以便我可以移动除本月之外的所有内容。我仍在研究和调查,所以如果我能找到这篇缺失的文章,我会确保更新我的帖子。谢谢大家的帮助

将上一次修改的文件保留在本月的一种方法是使用一个小助手函数,将LastWriteTime日期格式化为字符串
yyy\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
    }

通常我会说这个问题太宽泛了,你应该把问题分解成更具体的问题,包括你尝试过的细节等等,但几个月前我写了一个脚本,基本上就是这么做的。如果您想制作自己的版本,它是,并且是。至少有数千个脚本可以实现您所描述的功能,其中许多可以在inet上使用。[咧嘴笑]你找过这样的人吗?什么东西没有按预期工作?你犯了什么错误?谢谢@zzbov的链接!我会看看它们,因为我认为它可能有我在排除本月遗漏的部分:)我感谢这些资源!你好@Lee_Dailey-你说得绝对正确,我承认我发布这篇文章时有点惊慌失措,因为我还在寻找可能的解决方案。我更新了我的帖子,找到了托马斯·莫勒(Thomas Maurer)创作的一个脚本,这有助于回答我的问题:)目前,我正试图找出如何排除本月。我正在查看前一个评论者提供的资源,希望能将其拼凑起来。@CPG-要过滤掉当前月份的文件,您需要决定如何定义“当前月份”。[grin]一种方法是使用datetime对象的
.Month
属性。另一种方法是使用
.AddDays(-30)
获取30天的日期,然后将其用于过滤器。
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
    }