1
function cee([string]$a,
[string]$b)
{
    Write-Host $a | ft
    $a.GetType()
    Write-Host $b | ft
    $b.GetType()
    Add-Type -Assembly System.IO.Compression.FileSystem
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($a,$b)
}

cee('C:\FAKE','C:\zipfile.zip')

When run in this manor it fails and errors with:

C:\FAKE'C:\zipfile.zip

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object >

True True String System.Object
Exception calling "CreateFromDirectory" with "2" argument(s): "The given >path's format is not supported." At line:10 char:9 + [System.IO.Compression.ZipFile]::CreateFromDirectory($a, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : NotSupportedException

If I store 'C:\FAKE\' and 'C:\zipfile.zip' as variables, manually running each line with the variables as input for $a and $b, it works just fine.

If I set [Parameter(Mandatory=$True)] , run the function, and enter C:\FAKE\ and C:\zipfile.zip at the prompt, it works just fine.

Zac Borders
  • 137
  • 1
  • 3
  • 12
  • @PetSerAl What search terms did you use to locate the other answer? Clearly your search-fu is much better than mine. Or did knowing what I did wrong allow you to more easily find it? I would prefer to do a better job searching than bog down the community with duplicates. – Zac Borders Jul 18 '16 at 17:24
  • I use `[powershell] parenthesis` in search. I is not my first time seeing this error in calling PowerShell function, so I already know what to search for. – user4003407 Jul 18 '16 at 17:32
  • @PetSerAl Thank you as well. – Zac Borders Jul 18 '16 at 17:35

1 Answers1

2

You are not correctly calling your function. You use a function as such:

cee 'C:\FAKE' 'C:\zipfile.zip'

Or more fully, by calling parameters by name:

cee -a 'C:\FAKE' -b 'C:\zipfile.zip'
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Ah, yes, thank you. I knew my syntax had to be off somewhere. It has been interesting getting used to the way powershell does things. – Zac Borders Jul 18 '16 at 17:21