![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Re: passing [switch] parameters On Jul 28, 1:24 pm, <VR...@newsgroup.nospam> wrote: > Please help! > > So, I have this function X, which among other parameters accepts a switch. > It also calls another function, say Y, with exactly same set of > parameters/switches. The trick is that each one of those functions ensures > there are no additional arguments passed to it. > > So, calling X is easy: it's either X or X -mySwitch > The question is how to call Y from X, in such way so that if mySwitch is > true, it gets passed to Y, if false it doesn't. > > I am looking to do it in one line, since there another 5-10 parameters and > if I use an 'if' clause, things get messy. > > So, what I am looking for is the replacement for this: > if ($mySwitch) {Y -mySwitch} else {Y} > > Thanks in advance for any help. > VR > > Example: > > function X([switch] $mySwitch) > { > if ($args.Count -ne 0) {throw "no additional arguments allowed";} > > # do whatever ... > > # the line below is incorrect and causes the problem !!!! > # what is the correct way of passing that switch on, when the local > $mySwitch is true and not to pass it, when it's false? > Y -mySwitch $mySwitch } > > function Y([switch] $mySwitch) > { > # the line below will throw, since $args.Count is 1 > if ($args.Count -ne 0) {throw "no additional arguments allowed";} > > # do whatever ... > > } > > # call X > X -mySwitch Depending on the other parameters, this might get you what you want: Invoke-Expression ($MyInvocation.Line -replace "X", "Y") This calls Y in exactly the same way X was called. As long as Y is in the same scope as X, any non-switch parameters passed to X should be evaluated accurately in the call to Y. Good luck. Jeff |
My System Specs![]() |
| | #2 (permalink) |
| | Re: passing [switch] parameters <VRSki@newsgroup.nospam> wrote in message news:uIy9OaB0HHA.1168@TK2MSFTNGP02.phx.gbl... > Please help! > > So, I have this function X, which among other parameters accepts a switch. > It also calls another function, say Y, with exactly same set of > parameters/switches. The trick is that each one of those functions ensures > there are no additional arguments passed to it. > > So, calling X is easy: it's either X or X -mySwitch > The question is how to call Y from X, in such way so that if mySwitch is > true, it gets passed to Y, if false it doesn't. > > I am looking to do it in one line, since there another 5-10 parameters and > if I use an 'if' clause, things get messy. Switch parameters were meant to make it very simple: function X ([switch]$mySwitch) { Y -mySwitch:$mySwitch } Hope that helps, Jacques |
My System Specs![]() |
| | #3 (permalink) |
| | passing [switch] parameters Please help! So, I have this function X, which among other parameters accepts a switch. It also calls another function, say Y, with exactly same set of parameters/switches. The trick is that each one of those functions ensures there are no additional arguments passed to it. So, calling X is easy: it's either X or X -mySwitch The question is how to call Y from X, in such way so that if mySwitch is true, it gets passed to Y, if false it doesn't. I am looking to do it in one line, since there another 5-10 parameters and if I use an 'if' clause, things get messy. So, what I am looking for is the replacement for this: if ($mySwitch) {Y -mySwitch} else {Y} Thanks in advance for any help. VR Example: function X([switch] $mySwitch) { if ($args.Count -ne 0) {throw "no additional arguments allowed";} # do whatever ... # the line below is incorrect and causes the problem !!!! # what is the correct way of passing that switch on, when the local $mySwitch is true and not to pass it, when it's false? Y -mySwitch $mySwitch } function Y([switch] $mySwitch) { # the line below will throw, since $args.Count is 1 if ($args.Count -ne 0) {throw "no additional arguments allowed";} # do whatever ... } # call X X -mySwitch |
My System Specs![]() |
| | #4 (permalink) |
| | Re: passing [switch] parameters Of course! I am an iditiot -- I completely forgot about the good old colon! Thanks Jacues. "Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message news:uZMcJtC0HHA.5772@TK2MSFTNGP02.phx.gbl... > <VRSki@newsgroup.nospam> wrote in message > news:uIy9OaB0HHA.1168@TK2MSFTNGP02.phx.gbl... >> Please help! >> >> So, I have this function X, which among other parameters accepts a >> switch. It also calls another function, say Y, with exactly same set of >> parameters/switches. The trick is that each one of those functions >> ensures there are no additional arguments passed to it. >> >> So, calling X is easy: it's either X or X -mySwitch >> The question is how to call Y from X, in such way so that if mySwitch is >> true, it gets passed to Y, if false it doesn't. >> >> I am looking to do it in one line, since there another 5-10 parameters >> and if I use an 'if' clause, things get messy. > > Switch parameters were meant to make it very simple: > > function X ([switch]$mySwitch) { > Y -mySwitch:$mySwitch > } > > Hope that helps, > Jacques |
My System Specs![]() |
| | #5 (permalink) |
| | Re: passing [switch] parameters Jeff, Thanks for the post. VR "Jeff" <jeff.hillman@gmail.com> wrote in message news:1185523190.063165.182320@k79g2000hse.googlegroups.com... > On Jul 28, 1:24 pm, <VR...@newsgroup.nospam> wrote: >> Please help! >> >> So, I have this function X, which among other parameters accepts a >> switch. >> It also calls another function, say Y, with exactly same set of >> parameters/switches. The trick is that each one of those functions >> ensures >> there are no additional arguments passed to it. >> >> So, calling X is easy: it's either X or X -mySwitch >> The question is how to call Y from X, in such way so that if mySwitch is >> true, it gets passed to Y, if false it doesn't. >> >> I am looking to do it in one line, since there another 5-10 parameters >> and >> if I use an 'if' clause, things get messy. >> >> So, what I am looking for is the replacement for this: >> if ($mySwitch) {Y -mySwitch} else {Y} >> >> Thanks in advance for any help. >> VR >> >> Example: >> >> function X([switch] $mySwitch) >> { >> if ($args.Count -ne 0) {throw "no additional arguments allowed";} >> >> # do whatever ... >> >> # the line below is incorrect and causes the problem !!!! >> # what is the correct way of passing that switch on, when the local >> $mySwitch is true and not to pass it, when it's false? >> Y -mySwitch $mySwitch } >> >> function Y([switch] $mySwitch) >> { >> # the line below will throw, since $args.Count is 1 >> if ($args.Count -ne 0) {throw "no additional arguments allowed";} >> >> # do whatever ... >> >> } >> >> # call X >> X -mySwitch > > Depending on the other parameters, this might get you what you want: > > Invoke-Expression ($MyInvocation.Line -replace "X", "Y") > > This calls Y in exactly the same way X was called. As long as Y is in > the same scope as X, any non-switch parameters passed to X should be > evaluated accurately in the call to Y. > > Good luck. > > Jeff > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Passing parameters to a PS script | PowerShell | |||
| Help with PS wretched way of passing parameters to executables | PowerShell | |||
| Passing parameters to a cmdlet in a variable | PowerShell | |||
| Help with foreach-object and passing parameters | PowerShell | |||
| passing parameters to script (ps1) | PowerShell | |||