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