Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Problem with parameter passing?

Closed Thread
 
Thread Tools Display Modes
Old 10-06-2006   #1 (permalink)
=?Utf-8?B?TWF0dA==?=
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?
Old 10-06-2006   #2 (permalink)
=?Utf-8?B?TWF0dA==?=
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?

Old 10-06-2006   #3 (permalink)
Keith Hill [MVP]
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

Old 10-06-2006   #4 (permalink)
=?Utf-8?B?TWlrZSBCcmlkZ2U=?= bridgecanada _ com>
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
>

Closed Thread

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








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50