How to pass parameters to a function?

Posted by sbi on Server Fault See other posts from Server Fault or by sbi
Published on 2012-07-06T18:57:29Z Indexed on 2012/07/06 21:18 UTC
Read the original article Hit count: 195

Filed under:

I need to process an SVN working copy in a PS script, but I have trouble passing arguments to functions. Here's what I have:

function foo($arg1, $arg2)
{
  echo $arg1
  echo $arg2.FullName
}

echo "0: $($args[0])"
echo "1: $($args[1])"
$items = get-childitem $args[1] 
$items | foreach-object -process {foo $args[0] $_}

I want to pass $arg[0] as $arg1 to foo, and $arg[1] as $arg2. However, it doesn't work, for some reason $arg1 is always empty:

PS C:\Users\sbi> .\test.ps1 blah .\Dropbox
0: blah
1: .\Dropbox
C:\Users\sbi\Dropbox\Photos
C:\Users\sbi\Dropbox\Public
C:\Users\sbi\Dropbox\sbi
PS C:\Users\sbi>

Note: The "blah"parameter isn't passed as $arg1.

I am absolutely sure this is something hilariously simple (I only just started with doing PS and still feel very clumsy), but I have banged my head against this for more than an hour now, and I can't find anything.

© Server Fault or respective owner

Related posts about powershell