![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
| | #5 (permalink) |
| 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 Specs![]() |
| | #6 (permalink) |
| 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 Specs![]() |
![]() |
| 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 |