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