Powershell 之压缩

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Powershell 之压缩相关的知识,希望对你有一定的参考价值。

   当管理的系统越来越多,协助开发通过日志查找问题,相信是很多运维人员在日常工作中最头疼的问题,反复调试,来回调取日志,在这种情况下,系统的日志模式也必须调整为info级别,随之而来的也是日志文件的快速增长,导致要定期进行清理。


   于是乎,我采用了日志分离模式,将系统日志统一存放在一台Windows服务器上,因为接下来我要将这些日志文件定时进行压缩,收缩服务器存储空间。

function Auto-Zip{
    if(-not (Test-Path $args[0])){
        Write-Warning "请指定一个目录"
        break
    }
    dir $args[0]|?{$_ -is [System.IO.DirectoryInfo]}|
    foreach{
        $LogPath=$_.FullName
        $nowDate=(Get-Date).ToString().Split(" ")[0]
        dir  $LogPath|?{$_ -is [System.IO.FileInfo] -and $_.name -match "2015" -and $_.name -notmatch ".zip$" -and $_.name -notmatch ".rar$"}|
        foreach{
            $file=$_.FullName
            $zipname=$file+".zip"
            while(Get-Process -Name winrar -ErrorAction SilentlyContinue){
                sleep -milliseconds 500 
            }
            Write-Host "开始压缩! $file" -ForegroundColor Green
            winrar a $zipname $file -ibck -t -df
        }
    }
}

#调用,假如你的日志存放在D:\Logs\nginx

Auto-Zip D:\Logs


本文出自 “bxing” 博客,请务必保留此出处http://bxing.blog.51cto.com/1677994/1875905

以上是关于Powershell 之压缩的主要内容,如果未能解决你的问题,请参考以下文章

Powershell 之压缩

使用 7zip 压缩 powershell 5.0

如何在 powershell 中隐藏进度条(压缩)?

在不压缩的 Powershell 上使用 7zip

无法使用 Powershell 2.0 压缩文件

使用 powershell 压缩旧文件