0

I have a problem in that my PowerShell Archive archive script crashes after consuming my entire machines memory using the Compress-Archive cmdlet.

The for loop added below really helped by only dealing with a few files at a time, but I am still getting "Out of Memory" errors on my large files I am trying to compress.

Is setting max PowerShell memory still an option? I understand set max memory may not work. Also, I see garbage collection can be a solution. Where would one place the garbage collection and what would the appropriate syntax be? [System.GC]::Collect()?

$ArchiveContents = Get-ChildItem -Path C:\OneDrive | Sort-Object -Descending
for ($i=0; $i -le $ArchiveContents.Count; $i=$i+10) {
    Compress-Archive -Path $ArchiveContents.FullName[$i..($i+9)] -Update -DestinationPath \\Server011\Backup_Archive\CompanyData\Daily\OneDrive.zip -CompressionLevel Optimal
}
if (-not $?) {
    $SMTPServer = "smtp.gmail.com"
    #Redacted ?
    $smtp.Send($message)
} else {
    $SMTPServer = "smtp.gmail.com"
    #Redacted?
    $smtp.Send($message)
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user711393
  • 41
  • 2
  • Consider the option to use a dedicated command line tool (7zip has one) over `Compress-Archive`. You might get it to work with `Compress-Archive` eventually, but if it (*ever*) gives Out-of-Memory errors then I'd say it's not exactly ready for prime time. – Tomalak Jan 14 '19 at 16:50
  • Yes, I noticed there is a request on UserVoice for the out of Memory Issues to be addressed, so you may be right that it is not ready for Prime Time. I am familar with 7Zip but I was experiencing some permission issues with it as well. I might give it another go however. – user711393 Jan 14 '19 at 16:59
  • Should be a one-liner, since there is no need to split up the files you want to compress, and when you use the update operation (`7z u`) it will skip files that haven't changed. In place of `$?`, use a `$LASTEXITCODE` check. – Tomalak Jan 14 '19 at 17:03
  • Thanks so much! That will be very helpful! – user711393 Jan 14 '19 at 17:11

0 Answers0