如何在 Windows 中压缩超过 30 天的日志文件? [英] how to compress log files older than 30 days in windows?

查看:88
本文介绍了如何在 Windows 中压缩超过 30 天的日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写下面的powershell脚本来压缩超过30天的日志:-

I write below powershell script to compress logs older than 30 days:-

$LastWrite=(get-date).AddDays(-30).ToString("MM/dd/yyyy")

Get-ChildItem -Filter "server.log*" -Recurse -File | Where-Object 
{$_.LastWriteTime -le $LastWrite} 

现在,我无法在 powershell 中获取压缩命令,通过该命令我可以压缩(zip/tar)超过 30 天的 server.log* 文件.期待一个我可以通过在上面的命令中添加管道符号来使用的命令.

Now, I am unable to get compressed command in powershell via which I can compress(zip/tar) the server.log* files older than 30 days. Expecting a single command which i can use by adding a pipe sign in the above command.

推荐答案

如果您有 PowerShell 5 或更高版本,您可以使用 Compress-Archive cmdlet 来压缩文件:

You can use the Compress-Archive cmdlet to zip files if you have PowerShell version 5 or above:

$LastWrite = (get-date).AddDays(-30)

$Files = Get-ChildItem -Filter "server.log*" -Recurse -File | Where-Object {$_.LastWriteTime -le $LastWrite}

ForEach ($File in $Files) {
    $File | Compress-Archive -DestinationPath "$($File.fullname).zip"
}

这篇关于如何在 Windows 中压缩超过 30 天的日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆