1

Compress-Archive states that the Path parameter accepts a comma separated list of directories.

-Path Specifies the path or paths to the files that you want to add to the archive zipped file. To specify multiple paths, and include files in multiple locations, use commas to separate the paths.

The following code builds a comma separated list of paths, but results in an error stating the path is not valid.

Code

$now = Get-Date
Write-Host "Zipping logs... $now"
$dirZip = @()
                
foreach ($vm in $vms)
{
$vmName = $vm.name
$dirZip += "C:\Scripts\ClientLogs\$vmName"
}

$currentDate = Get-Date -Format "MM-dd-yyyy"
$zipDestinationPath = "C:\Scripts\" + $currentDate + "_Client_Logs.zip"
Compress-Archive -Path ($dirZip -join ',') -DestinationPath $zipDestinationPath -Force

Error

Compress-Archive : The path 'C:\Scripts\ClientLogs\Client1,C:\Scripts\ClientLogs\Client2,C:\Scripts\ClientLogs\Client3,C:\Scripts\ClientLogs\Client4,C:\Scripts\ClientLogs\Client5' either
does not exist or is not a valid file system path.
At C:\Scripts\VM Copy Files.ps1:169 char:5
+ ...             Compress-Archive -Path ($dirZip -join ',') -DestinationPa ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\Scripts\Clie...entLogs\Client5:String) [Compress-Archive], InvalidOperationException
    + FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Compress-Archive

Copying the "invalid path" from the error message and using it directly in a Compress-Archive works though...

Compress-Archive -Path C:\Scripts\ClientLogs\Client1,C:\Scripts\ClientLogs\Client2,C:\Scripts\ClientLogs\Client3,C:\Scripts\ClientLogs\Client4,C:\Scripts\ClientLogs\Client5 -DestinationPath C:\temp\test.zip

I've tried building the Path value by just concatenating strings and passing it as a variable, but that resulted in the same error.

$now = Get-Date
Write-Host "Zipping logs... $now"

foreach ($vm in $vms)
{
$vmName = $vm.name
$dirZip = $dirZip + "C:\Scripts\ClientLogs\$vmName,"
}
$currentDate = Get-Date -Format "MM-dd-yyyy"
$zipDestinationPath = "C:\Scripts\" + $currentDate + "_Client_Logs.zip"
$dirZip = $dirZip.trimend(",")
Compress-Archive -Path $dirZip -DestinationPath $zipDestinationPath -Force      
  • Not at my computer to test, but I'm pretty sure its because you dont have a comma separated list of paths, you have one path containing a string with commas in it.... – Scepticalist Jan 24 '23 at 16:44
  • 1
    The docs are needlessly confusing here by trying to be helpful. `Path` accepts an array of strings, which you *can* specify through multiple, comma-separated strings if you're putting in literals, but not as the contents of one string. In your case, it should be as simple as specifying `-Path $dirZip` in your first snippet and leaving out the `-join` altogether. – Jeroen Mostert Jan 24 '23 at 16:46
  • That did it. I currently lack the understanding of why passing the array of directories is interpreted as a comma separated list by Compress-Archive, but that's why I'm here. – BarelyAdequateSoftwareTest Jan 24 '23 at 17:03
  • 1
    It isn't interpreted as a comma-separated list, that's the trick. It accepts an array; in PowerShell, you can construct arrays by using the comma (`'a', 'b', 'c'`), but this is not the same as a comma-separated list of values as a single string (that's `'a, b, c'`). The docs are pointing out the former syntax, which is not specific to `Compress-Archive` but work for anything accepting an array. But since you are already creating an array directly, you have nothing to do with any of this. – Jeroen Mostert Jan 24 '23 at 17:06
  • When looking at the docs you would see `[-Path] ` which means the cmdlet can take an array of strings. When you use `-join` you're actually converting that array into a single string delimited by commas and the cmdlet doesn't know how to handle that. Basically what @JeroenMostert pointed out in his helpful comments – Santiago Squarzon Jan 24 '23 at 22:36

0 Answers0