0

I am attempting to write a powershell function that takes 3 parameters

Function CopyVHD ($filename, $sourcevhd, $destination)
{
    echo "filename is $filename"
    echo "source is $sourcevhd"
    echo "destination is $destination"
    # Validate that the VHD doesn't exist on remote
    if ( Test-Path "$sourcevhd\$filename" )
    {
        echo "File $filename already exists at $destination"
    }
    else 
    {
      echo "copying $sourcevhd\\$filename to $destination"
      Copy-Item $sourcevhd\$filename "$destination" -Recurse
    }

}

I then pass in 3 parameters

CopyVHD("foo.vhd","c:\","d:\")

Why does powershell combine the 3 parameters into 1 ? If you notice at the output below, the variable filename has consumed all 3 parameters while the parameters source and destination are blank.

PS C:\Windows\system32> C:\Users\example\Documents\closed-pile.ps1
newest vhd is foo.vhd
filename is foo.vhd C:\ D:\
source is 
destination is 
copying \\foo.vhd C:\ D:\ to 
Copy-Item : Cannot find drive. A drive with the name '\foo.vhd C' does not exist.
At C:\Users\example\Documents\closed-pile.ps1:28 char:7
+       Copy-Item $sourcevhd\$filename "$destination" -Recurse
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\foo.vhd C:String) [Copy-Item], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

I have tried different variable names incase I am mistakenly using reserved words. I have tried different space syntaxes.

CopyVHD ("foo.vhd","c:\","d:\")
CopyVHD("foo.vhd","c:\","d:\")
CopyVHD("foo.vhd", "c:\", "d:\")
CopyVHD( "foo.vhd", "c:\", "d:\" )

I have another function in the same script which accepts 2 parameters properly. Why won't this function work?

Update

I've tried the following syntax which is shown in the linked SO question. It gives the same failure

CopyVHD "foo.vhd", "C:\", "D:\"

Update2

Also tried in single quotes

CopyVHD 'foo.vhd', 'C:\', 'D:\'

Function still does not recognize 3 parameters

PS C:\Windows\system32> C:\Users\sowen\Documents\closed-pile.ps1
filename is foo.vhd C:\ D:\
source is 
destination is 
spuder
  • 17,437
  • 19
  • 87
  • 153
  • That's not how you call a powershell function. You want `CopyVHD "arg1", "arg2", "arg3"`. – Etan Reisner Apr 07 '15 at 20:58
  • I've realised that my duplicate flag isn't valid, as the question I linked to was about VS build events. However there is an answer there that will solve your question. – DeanOC Apr 07 '15 at 21:20
  • You dont call functions in powershell the same as you do in VB. FoxDeploy is correct in his answer for calling powershell functions. – Dane Boulton Apr 07 '15 at 21:25

1 Answers1

5

You don't separate parameters in PowerShell with commas, commas are used to show multiple entries for one parameter.

The way you should be calling this function is to separate the parameters with a space. Or better yet, provide the parameter name.

CopyVHD A B C

OR

CopyVHD -filename A -sourcevhd B -destination C

>filename is a
source is b
destination is c 
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48