Script to create archive using powershell and 7zip

前端 未结 3 1977
天命终不由人
天命终不由人 2021-01-21 09:15

We have several servers that write log files to C:\\Logs on a daily basis. Every month, a script is supposed to run to identify files older than 30 days, archive them, and delet

3条回答
  •  面向向阳花
    2021-01-21 09:53

    Do you have to use 7zip?

    I have done the same task using dotnetzip and was able to save the folder structure.

    Here is my code, you can use this and add your date logic to it:

    [System.Reflection.Assembly]::LoadFrom("c:\\\User\\bin\\Ionic.Zip.dll");
    $zipfile = new-object Ionic.Zip.ZipFile("C:\user\temp\logs\TestZIP.zip");
    
    $directory = "C:\user\temp\logs\"
    $children = get-childitem -path $directory
    foreach ($o in $children)
    {
       if($o.Name.EndsWith(".log")){
          $e = $zipfile.AddFile($o.FullName)
       }
    }
    $zipfile.Save()
    $zipfile.Dispose()
    

提交回复
热议问题