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 - passing [switch] parameters

Reply
 
Old 07-27-2007   #1 (permalink)
Jeff


 
 

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 SpecsSystem Spec
Old 07-27-2007   #2 (permalink)
Jacques Barathon [MS]


 
 

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 SpecsSystem Spec
Old 07-28-2007   #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 SpecsSystem Spec
Old 07-28-2007   #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 SpecsSystem Spec
Old 07-28-2007   #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 SpecsSystem Spec
Reply

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


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