Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum 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

HowTo: Add a switch to function/filter/script

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-29-2006   #1 (permalink)
Brandon Shell
Guest


 

HowTo: Add a switch to function/filter/script

I know how to pass a parameter, but I am unsure if there is an easier way to
add just a switch to a function.

Example 1:
Function foo {
Param([string]$server,[string]$file)
$server
$file
}
This gives me the option to either position specific or -server/-file
parameters

Function foo2 {
$args[0]
$args[1]
}
This only lets me do position specific, but doest allow to preset parameters
(i.e. -server)

------------------------

What I would like is to be able to add a switch like -debug
foo -server "myserver" -file "hello.txt" -debug

How would I accomplish this (preferably without having to parse $args.)


My System SpecsSystem Spec
Old 12-29-2006   #2 (permalink)
Tom G.
Guest


 

Re: HowTo: Add a switch to function/filter/script

You can define a parameter as being of type [switch]. For example,

Function foo
{
Param([string]$server,[string]$file,[switch]debug)
$server
$file
if $debug { write-host "The debug switch is present." }
}

Tom G.

--



Brandon Shell wrote:

> I know how to pass a parameter, but I am unsure if there is an easier
> way to add just a switch to a function.
>
> Example 1:
> Function foo {
> Param([string]$server,[string]$file)
> $server
> $file
> }
> This gives me the option to either position specific or -server/-file
> parameters
>
> Function foo2 {
> $args[0]
> $args[1]
> }
> This only lets me do position specific, but doest allow to preset
> parameters (i.e. -server)
>
> ------------------------
>
> What I would like is to be able to add a switch like -debug
> foo -server "myserver" -file "hello.txt" -debug
>
> How would I accomplish this (preferably without having to parse
> $args.)

My System SpecsSystem Spec
Old 12-29-2006   #3 (permalink)
Brandon Shell
Guest


 

Re: HowTo: Add a switch to function/filter/script

Thanks... that answers my question... I figured it was easy
"Tom G." <thomglow@lycos.com> wrote in message
news:eqKT6R2KHHA.3872@TK2MSFTNGP06.phx.gbl...
> You can define a parameter as being of type [switch]. For example,
>
> Function foo
> {
> Param([string]$server,[string]$file,[switch]debug)
> $server
> $file
> if $debug { write-host "The debug switch is present." }
> }
>
> Tom G.
>
> --
>
>
>
> Brandon Shell wrote:
>
>> I know how to pass a parameter, but I am unsure if there is an easier
>> way to add just a switch to a function.
>>
>> Example 1:
>> Function foo {
>> Param([string]$server,[string]$file)
>> $server
>> $file
>> }
>> This gives me the option to either position specific or -server/-file
>> parameters
>>
>> Function foo2 {
>> $args[0]
>> $args[1]
>> }
>> This only lets me do position specific, but doest allow to preset
>> parameters (i.e. -server)
>>
>> ------------------------
>>
>> What I would like is to be able to add a switch like -debug
>> foo -server "myserver" -file "hello.txt" -debug
>>
>> How would I accomplish this (preferably without having to parse
>> $args.)


My System SpecsSystem Spec
Old 12-29-2006   #4 (permalink)
Sung M Kim
Guest


 

Re: HowTo: Add a switch to function/filter/script

For a various ways to use "switch" in your function,
you can refer to this blog entry
http://msgoodies.blogspot.com/2006/1...arameters.html

My System SpecsSystem Spec
Old 12-29-2006   #5 (permalink)
Keith Hill [MVP]
Guest


 

Re: HowTo: Add a switch to function/filter/script

"Tom G." <thomglow@lycos.com> wrote in message
news:eqKT6R2KHHA.3872@TK2MSFTNGP06.phx.gbl...
> You can define a parameter as being of type [switch]. For example,
>
> Function foo
> {
> Param([string]$server,[string]$file,[switch]debug)
> $server
> $file
> if $debug { write-host "The debug switch is present." }
> }


And even cooler is that you can pass this switch parameter to another
cmdlet's switch parameter e.g.:

function foo([string]$server, [string]$file, [switch]$whatif) {
some-cmdlet $server, $file -WhatIf:$whatif
}

--
Keith



My System SpecsSystem Spec
Old 12-31-2006   #6 (permalink)
radean@gmail.com
Guest


 

Re: HowTo: Add a switch to function/filter/script

This should do what you are looking for.

-Bob

Example 1:
Function foo {
Param([string]$server,[string]$file,[switch]$debug)
if ($debug) { "Do something" }
$server
$file
}



Brandon Shell wrote:
> I know how to pass a parameter, but I am unsure if there is an easier way to
> add just a switch to a function.
>
> Example 1:
> Function foo {
> Param([string]$server,[string]$file)
> $server
> $file
> }
> This gives me the option to either position specific or -server/-file
> parameters
>
> Function foo2 {
> $args[0]
> $args[1]
> }
> This only lets me do position specific, but doest allow to preset parameters
> (i.e. -server)
>
> ------------------------
>
> What I would like is to be able to add a switch like -debug
> foo -server "myserver" -file "hello.txt" -debug
>
> How would I accomplish this (preferably without having to parse $args.)


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto: pass all args to a sub function? Staffan Gustafsson PowerShell 3 05-23-2007 04:48 AM
Phishing filter cannot function,help me. wangxy Vista security 5 05-09-2007 05:37 PM
HowTo: Convert Function to script Brandon Shell PowerShell 3 12-03-2006 11:20 PM
Info: Filter vs Function Brandon Shell PowerShell 3 08-24-2006 08:35 AM
filter function not working? Martin Hueser PowerShell 8 08-17-2006 07:49 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 51