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
6
  • This appears to be a duplicate of stackoverflow.com/questions/4225748/… Commented Jul 12, 2013 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. Commented Jul 12, 2013 at 20:58
  • but it is a duplicate of stackoverflow.com/questions/4988226/… and stackoverflow.com/questions/15504980/… and probably many more :) Commented Jul 12, 2013 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! Commented Jul 12, 2013 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. Commented Jul 12, 2013 at 21:13

1 Answer 1

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. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.