1

I found powershell has compress-archive cmdlet from How do I exclude a folder in compress-archive; however, if I pass powershell argument into the -exclude parameter, it is not working. If it call from powershell variable it is working. Am I missing something? Please advise.

This script is not working. Here is powershell parameters

.\createzipfile.ps1 "C:\temp" "C:\Myzipdir" myzipfile.zip '"web.config","PageBase.master"'

Param
(
   [string]$source,
   [string]$destination,
   [string]$zipfilename,
   [string]$excludefilepattern
)
cls
$zipfile = $destination + "\" + $zipfilename

$files = Get-ChildItem -Path $source -Exclude $excludefilepattern
# compress
#Compress-Archive -Path $files -DestinationPath $zipfile -CompressionLevel Fastest
Compress-Archive -Path $files -DestinationPath $zipfile -CompressionLevel Fastest

This script is working but I really do need to have -exclude passing from powershell parameter

.\createzipfile.ps1 "C:\temp" "C:\Myzipdir" myzipfile.zip

Param
(
    [string]$source,
    [string]$destination,
    [string]$zipfilename

)
cls
$zipfile = $destination + "\" + $zipfilename
$exclude = ("web.config","PageBase.master")
$files = Get-ChildItem -Path $source -Exclude $exclude
# compress

Compress-Archive -Path $files -DestinationPath $zipfile -CompressionLevel Fastest

Best Regards, Andy Pham

ArcSet
  • 6,518
  • 1
  • 20
  • 34
Andy Pham
  • 67
  • 4
  • Don't quote the last argument `'"web.config","PageBase.master"'` should be `"web.config","PageBase.master"` – Matt Oct 02 '17 at 19:06

2 Answers2

0

I'm not in a position to test this 100% at the moment, but I'm willing to be this will sort it for you.

In your first example, your parameter for your exclude is: [string]$excludefilepattern

This will accept a string, and what you're passing in is a string made to look like an array. I'm assuming this is because you were getting an error when trying to pass in a real array?

The fix is to denote that parameter as being able to accept an array of strings:

[string[]]$excludefilepattern

It'll then work when you supply an array (note I've removed the outer single quotes):

.\createzipfile.ps1 "C:\temp" "C:\Myzipdir" myzipfile.zip "web.config","PageBase.master"

What can help with this sort of thing, is checking the help pages for the cmdlets you're using. In this case with Get-ChildItem you'll note exclude listed as [-Exclude <String[]>]. The square brackets let you know it wants an array of whatever that type is (string in this case.)

Windos
  • 1,796
  • 1
  • 13
  • 19
  • 1
    Yes, -exclude parameter from cmdlet Get-ChildItem is array. My script is working as expected. Silly mistake -:). Thanks so much Windos – Andy Pham Oct 03 '17 at 23:25
0

Since you have not mentioned the positions of each parameter, better use it named. When you do it like this, there will be no ambiguity with regards to the multiple file patterns you pass. And of course, don't enclose the patterns in a single quote. Like:

.\createzipfile.ps1 -Source "C:\temp" -Destination "C:\Myzipdir" -Zipfilename myzipfile.zip -ExcludeFilePattern "web.config","PageBase.master"

Also, the file patterns cannot be a string. Since you are going to pass multiple, it has to be an array. Better not type caste the $ExcludeFilePattern inside the param block

Sid
  • 2,586
  • 1
  • 11
  • 22