![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||