Here is my question;
$source = "\\Somewhere\Overtherainbow\something.exe"
$destinationSource = "C:\temp"
Function MyCopyFunction([string]$from, [string]$to)
{
$src= $source+$from
$dest = $destinationSource+$to
Copy-Item -Path $src -Destination $dest -Recurse
}
Function MyFunction([string]$p1, [string]$p2)
{
MyCopy($p1, $p2)
}
switch("1"){
"1" {
MyFunction("Dir1","Dir2") break;
}
}
Simple right ? Why is it when the "MyCopyFunction(p1,p2)" get called with 2 parameters it complains that the second parameter does not exist. But if I turn "MyCopyFunction($p1)" with only 1 parameter instead of two and supply the -Destination value manually, no issues what is the source of the issue here?
Here is the exception.... Copy-Item -Path $from -Destination $to -Recurse CategoryInfo : ObjectNotFound: ( :String) [Copy-Item], ItemNotFoundExcptionFullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
The main problem is here,
MyCopyFunction(p1,p2)
When you call the function like this, PowerShell treats the input as a single array containing two elements and pass it positionally to the first variable(In this case it's $from
). Instead of this, you should change the call site to MyCopyFunction "Dir1" "Dir2"
to make it work.