![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Problem with parameter passing? Hi, either I'm missing something here or Powershell RC2 is not behaving well. When I try to pass two parameters to a function, they seem to get concatenated. Here is my code: function get-two-params { param($first, $second) write-host $first -foregroundcolor green write-host $second -foregroundcolor red } get-two-params("first","second") This outputs: PS C:\> check-getparams.ps1 first second (both in green). Why does it not recognize the other parameter? |
| | #2 (permalink) |
| Guest | RE: Problem with parameter passing? ok...well...it works when I use a space instead of a comma between the parameters: PS C:\>get-two-params "first" "second" first second But isn't that a little bizarre? I would expect a comma to function properly and the space to cause an error... Thanks. "Matt" wrote: > Hi, either I'm missing something here or Powershell RC2 is not behaving well. > > When I try to pass two parameters to a function, they seem to get > concatenated. > > Here is my code: > > function get-two-params { > param($first, $second) > > write-host $first -foregroundcolor green > write-host $second -foregroundcolor red > > } > > get-two-params("first","second") > > > This outputs: > > PS C:\> check-getparams.ps1 > first second > > > (both in green). Why does it not recognize the other parameter? |
| | #3 (permalink) |
| Guest | Re: Problem with parameter passing? "Matt" <Matt@discussions.microsoft.com> wrote in message news:3F3D7400-1D08-44D5-B843-708752D279A6@microsoft.com... > ok...well...it works when I use a space instead of a comma between the > parameters: > > PS C:\>get-two-params "first" "second" > first > second I keep getting bit by this one and it is caused by my programming background. The thing to keep in mind is that unless you are calling a ..NET method (or COM method) it is best to think of the parameters to functions as just like arguments to cmdlets. Don't use the parens because that kicks PoSH into expression mode so you wind up getting a single element that is an array containing the comma separated values in it. -- Keith |
| | #4 (permalink) |
| Guest | Re: Problem with parameter passing? Hi- I've wasted hours with PowerShell trying to debug problems caused by arrays doing different things in different contexts. Is there a comprehensive guide to "how to avoid screwing up with arrays in PowerShell"? I'm not talking about the basic syntax, but all these arcane rules for using them. Sometimes they get demoted to objects (e.g. if they have a length of one). Some arrays generate an error where you wouldn't expect (e.g. passing a zero-length array to some functions, where you would expect nothing to happen). Sometimes they get passed with commas (e.g. to a COM object). Sometimes they get passed with spaces (e.g. to a function). Sometimes they get broken out and are passed as individual object (e.g. to a .Net call). To hack around these "intended features", sometimes you can use the ',' operator. Or sometimes the '@()' operator---but not always (e.g. when calling a .Net call). I'm completely confused. Can someone concicely state the philosophy behind all these special cases? Why does it have to be so bizarre? Were these different contexts supposed to somehow make programming simpler? Sorry if I'm being daft here. Maybe there's a guiding principle that I'm missing. But as it is, I can't help but feel like it's a bit of a mess! -Mike "Keith Hill [MVP]" wrote: > "Matt" <Matt@discussions.microsoft.com> wrote in message > news:3F3D7400-1D08-44D5-B843-708752D279A6@microsoft.com... > > ok...well...it works when I use a space instead of a comma between the > > parameters: > > > > PS C:\>get-two-params "first" "second" > > first > > second > I keep getting bit by this one and it is caused by my programming > background. The thing to keep in mind is that unless you are calling a > .NET method (or COM method) it is best to think of the parameters to > functions as just like arguments to cmdlets. Don't use the parens because > that kicks PoSH into expression mode so you wind up getting a single element > that is an array containing the comma separated values in it. > > -- > Keith > |
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Invoke-Expression parameter Passing | James | PowerShell | 4 | 11-02-2007 03:44 AM |
| Passing filename as parameter | anth | PowerShell | 3 | 09-28-2007 10:24 PM |
| Passing a HashTable object as a parameter to a script | Brillig | PowerShell | 8 | 02-07-2007 04:45 PM |
| Function Parameter passing wrong or am I ? | joergH | PowerShell | 4 | 01-23-2007 05:16 PM |
| Passing parameter from one Page to another | Johann MacDonagh | Avalon | 3 | 04-10-2006 05:01 PM |