Compressing files in Powershell 4

Asked

Viewed 33 times

0

I have Windows with Powershell 4 on a server. I would like to compress files, and for that I know the Compress-Archive of Powershell. How do I install it? You need to touch . NET Framework?

In principle installed it with Install-Module, but this is not available in this version of Powershell.

1 answer

0


Since I couldn’t install Compress-Archive on PS4, I solved my question by using the method CopyHere, as follows:

    $fileAge = 180

    [byte[]] $emptyZipBytes = @(80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

    $logFiles = Get-ChildItem C:\logs  | Where-Object  {$_.LastWriteTime -lt (Get-Date).AddDays(-$fileAge)}
    
    $logFilePaths = @()
    
    foreach($logFile in $logFiles) {
        $logFilePaths += $logFile.FullName
        }
    
    $zipFile = C:\Archive\logs.zip

    If (![io.file]::Exists($zipFile)) {
        [io.file]::WriteAllBytes($zipFile,$emptyZipBytes)
    }

    If ($logFilePaths.Count -gt 0) {
        foreach ($fPath in $logFilePaths) {
          (New-Object -Com Shell.Application).NameSpace($zipFile).CopyHere($fPath, 0x14)
          
          Get-Process powershell -ea 0 | Where-Object MainWindowTile -match "(\d{1-3} concluído)|(Comprimindo...)" | Wait-Process
        }

        Remove-Item $logFilePaths
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.