Powershell 之壓縮

   當管理的系統愈來愈多,協助開發經過日誌查找問題,相信是不少運維人員在平常工做中最頭疼的問題,反覆調試,來回調取日誌,在這種狀況下,系統的日誌模式也必須調整爲info級別,隨之而來的也是日誌文件的快速增加,致使要按期進行清理。nginx


   因而乎,我採用了日誌分離模式,將系統日誌統一存放在一臺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
相關文章
相關標籤/搜索