Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - how do you pass named params to funcs by way of variables

Reply
 
Old 08-19-2007   #1 (permalink)
Bob Landau


 
 

how do you pass named params to funcs by way of variables

I tried to make the subject make sense but there is so little room to type.


What I'm trying to do is to take the input from the PS prompt; process this
and pass this on to another function which has named parameters. Regardless
of what I've done though the parameters are being bound by position not by
name.


Here is an example based on the helpfile about_function that will show what
I mean.

function name_age
{
param([string]$first = "unknown", [string]$last = "unknown", [int]$age =
0)

Write-Output "In name_age $last, $first : $age"
}

echo 'calling name_age with hard coded parameters'
name_age -age 22 -last brown -first randy

# The above works as expected the parameters are bound by name

$t = '-age 22 -last brown -first randy'
name_age $t

In this case the parameters are bound by position. The results are no
diffierent if you were to break the above string $t into 3 named=value
pairs or 6 individual parameters

Sorry if this was asked previously I was not able to find the answer

thx
bob



My System SpecsSystem Spec
Old 08-19-2007   #2 (permalink)
Brandon Shell


 
 

Re: how do you pass named params to funcs by way of variables

Try invoke-expression "name_age $t"

More Info
PS> get-help invoke-expression -detailed

"Bob Landau" <BobLandau@discussions.microsoft.com> wrote in message
news:1CBE320A-5BE5-4ACD-AAF2-C690FF0A4E92@microsoft.com...
>I tried to make the subject make sense but there is so little room to type.
>
>
> What I'm trying to do is to take the input from the PS prompt; process
> this
> and pass this on to another function which has named parameters.
> Regardless
> of what I've done though the parameters are being bound by position not by
> name.
>
>
> Here is an example based on the helpfile about_function that will show
> what
> I mean.
>
> function name_age
> {
> param([string]$first = "unknown", [string]$last = "unknown", [int]$age
> =
> 0)
>
> Write-Output "In name_age $last, $first : $age"
> }
>
> echo 'calling name_age with hard coded parameters'
> name_age -age 22 -last brown -first randy
>
> # The above works as expected the parameters are bound by name
>
> $t = '-age 22 -last brown -first randy'
> name_age $t
>
> In this case the parameters are bound by position. The results are no
> diffierent if you were to break the above string $t into 3 named=value
> pairs or 6 individual parameters
>
> Sorry if this was asked previously I was not able to find the answer
>
> thx
> bob
>
>


My System SpecsSystem Spec
Old 08-19-2007   #3 (permalink)
Bob Landau


 
 

Re: how do you pass named params to funcs by way of variables

Thank you Brandon this does exactly what I asked for. I did look at this
cmdlett briefly and discounted prematurally. I used it like

name_age Invoke-Expression $t

Seems like Invoke-Expression is very similar to using executing dynamic SQL.
At least (until I'm told otherwise) I'll be considering to use it under
similar situations

bob

"Brandon Shell" wrote:

> Try invoke-expression "name_age $t"
>
> More Info
> PS> get-help invoke-expression -detailed
>
> "Bob Landau" <BobLandau@discussions.microsoft.com> wrote in message
> news:1CBE320A-5BE5-4ACD-AAF2-C690FF0A4E92@microsoft.com...
> >I tried to make the subject make sense but there is so little room to type.
> >
> >
> > What I'm trying to do is to take the input from the PS prompt; process
> > this
> > and pass this on to another function which has named parameters.
> > Regardless
> > of what I've done though the parameters are being bound by position not by
> > name.
> >
> >
> > Here is an example based on the helpfile about_function that will show
> > what
> > I mean.
> >
> > function name_age
> > {
> > param([string]$first = "unknown", [string]$last = "unknown", [int]$age
> > =
> > 0)
> >
> > Write-Output "In name_age $last, $first : $age"
> > }
> >
> > echo 'calling name_age with hard coded parameters'
> > name_age -age 22 -last brown -first randy
> >
> > # The above works as expected the parameters are bound by name
> >
> > $t = '-age 22 -last brown -first randy'
> > name_age $t
> >
> > In this case the parameters are bound by position. The results are no
> > diffierent if you were to break the above string $t into 3 named=value
> > pairs or 6 individual parameters
> >
> > Sorry if this was asked previously I was not able to find the answer
> >
> > thx
> > bob
> >
> >

>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
VBS Telnet to Server and Pass Variables for User and Password?? VB Script
Pop-up to enter variables to pass to script PowerShell
b_r funcs stopped working Vista hardware & devices


Vista Forums 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 Ltd

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