1

I am trying to pass in 2 argument to a PowerShell script which calls invoke-command and I'm trying to pass in multiple parameters. But when I do, it seems like both parameters are put into a single variable and I'm wondering why.

Here is the code for both programs:

POC.ps1:

param($filename, $user)

echo $filename
echo "This"
echo $user

$responseObject = Invoke-Command CAPTESTPK01 -FilePath .\validatePath.ps1  -ArgumentList($filename, $user) -AsJob 




while($responseObject.State -ne "Completed")
{

}

$result = Receive-Job -Id $responseObject.Id -Keep
echo $result

validatPath.ps1:

Param([string] $filename,
      [string] $user)

function ValidatePath( $filename, $user, $fileType = "container" )
{
    Write-Host "This is the file name: $filename"
    echo "This is user: $user"
    $fileExist = $null
    if( -not (test-path $filename -PathType $fileType) )
    {               
        throw "$user, the path $filename does not exist!"

    }
    else
    {
         Write-Host "This is the second part"
         echo $filename found!
    }
     Write-Host "This is the third part"
    return $fileExist
}


try
{

    ValidatePath($filename, $user)
}
catch
{
    $e = $_.Exception
    echo $e
}

Here is the output:

C:\Users
This
Blaine
This is the file name: C:\Users Blaine <--- This indicated both arguments are in one variable
This is user: 
This is the second part
This is the third part
C:\Users
Blaine
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • This appears to be a duplicate of http://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command – Eris Jul 12 '13 at 20:53
  • 1
    No, this is not a duplicate of that. This is my code. I am using -ArguementList, they are not. They reference it, but they are getting errors. I'm having a different issue. – BlackHatSamurai Jul 12 '13 at 20:58
  • but it is a duplicate of http://stackoverflow.com/questions/4988226/powershell-multiple-function-parameters and http://stackoverflow.com/questions/15504980/passing-parameter-to-powershell-function-not-working and probably many more :) – Lars Truijens Jul 12 '13 at 20:59
  • Meh, it's debatable. While it is the same issue, or same fix, mine is questioning 1. My specific code; 2. The use of `-ArgumentList`. I wasn't sure if that had something to do with it ;) Thank you for your help though! – BlackHatSamurai Jul 12 '13 at 21:02
  • May I suggest that next time you try to strip your code to a minimum where it still shows your problem? That might help you find the answers yourself faster and helps question here to the point. – Lars Truijens Jul 12 '13 at 21:13
  • You may. The reason I post it all is because I'm never sure what is needed. I've left things off before, and people yell at me for not posting properly. Thanks for the feed back :) – BlackHatSamurai Jul 12 '13 at 21:15

1 Answers1

2

Arguments to PowerShell functions should not be put in parenthesis when called. It should be ValidatePath $filename $user. What you have written results in calling ValidatePath with just one parameter and $filename being an array of two values, namely the filename and the user.

BTW: When calling .Net methods in PowerShell you do need the parenthesis. :)

Lars Truijens
  • 42,837
  • 6
  • 126
  • 143