header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Subject: parameter defaults
Prev Next
You are not authorized to post a reply.

Author Messages
mhensleyUser is Offline
New Member
New Member
Posts:14

07/17/2008 1:51 PM  

I want to create a script (e.g. "myscript.ps1") that takes three optional parameters ("myparm1, myparm2, and myparm3"). I'd like to see the following behaviours:

.\myscript.ps1 -myparm2

(this would invoke myscript.ps1 with myparm2 set to $True, and myparm1 and myparm3 set to $False).

I can't seem to quite get it right. I've tried:

param($myparm1=$False, $myparm2=$False, $myparm3=$False)

and while that makes the parameters all default to $False as desired, I have to invoke it with

.\myscript.ps1 -myparm2 $True

rather than the way I want.

Any advice?

 

glnsizeUser is Offline
Shell Enthusiast
Shell Enthusiast
Posts:62


07/17/2008 2:36 PM  

It looks like your trying to establish a series of Boolean values.  If that's the case try...

 

function test {
    param( [switch]$test1=($FALSE), [switch]$test2=($FALSE), [switch]$test3=($FALSE))
    if ($test1) {
        Write-Host "test one true"
    }
    if ($test2) {
        Write-Host "test two true"
    }
    if ($test3) {
        Write-Host "test three true"
    }
}

 ~Glenn

halr9000User is Offline
CLI Addict
CLI Addict
Posts:273


07/17/2008 2:59 PM  
Incidentally, the parens around $false are not required. e.g.

param ( [switch]$param1 = $true, [switch]$param2 = $false )

Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast (http://powerscripting.net)
Author, TechProsaic (http://halr9000.com)
mhensleyUser is Offline
New Member
New Member
Posts:14

07/17/2008 3:06 PM  

Thanks, that works perfectly!

Now I just need to take it apart and figure out HOW it work. Looks like there are a few PowerShell things I can learn from it.

Does "[switch]" mean you are casting the test1 as a switch TYPE? Not sure I get that.

And, why the parentheses around "$FALSE"?

I should probably figure it out for myself, but if you (or anyone else) wants to post a quick explanation, maybe others would benefit.

I really do appreciate the solution. It does exactly what I wanted.

 

glnsizeUser is Offline
Shell Enthusiast
Shell Enthusiast
Posts:62


07/17/2008 3:38 PM  

 

Sorry for the confusion… [switch] does caste a given parameter in to Boolean value. If you wanted to establish a default value for any parameter then =() will do so.

in this case ...

param( [switch]$test1, [switch]$test2, [switch]$test3)

Is more then sufficient since [switch] is false by default.

You have more than likely seen the following.

param ($value=$(throw '$value is required!')) 

In that case, if $value is null then the default value is invoked.  The default value being  a throw statement.  You can use this technique for almost anything, IE...

param ($dc=(([ADSI]"LDAP://rootDSE").dnshostname) #tGgggggrrrrreat script BSonPOSH

In this case if an [ADSI] object is supplied via the -$dc parameter then that object is used. If none is provided then the posh code in () is applied establishing a default parameter.

~Glenn

 

halr9000User is Offline
CLI Addict
CLI Addict
Posts:273


07/17/2008 3:54 PM  
Again, extra set of parens. :I'll let you figure it out, Glenn. :D

@mhensley, when you put any type name in brackets and prepend that in front of a variable name (or a constant for that matter), then you are constraining the variable to that type.

PS > [int]$i = "dog"
Cannot convert value "dog" to type "System.Int32". Error: "Input string was not in a correct
 format."
At line:1 char:8
+ [int]$i <<<<  = "dog"


It works for simple types like [int] and [string] or [switch] (which you now know is a boolean true/false type) but it also works for complex types like this:

PS > [xml]$x = gc scan2.xml
PS > $x.nmaprun.args
C:\Program Files\Nmap\nmap.exe -oX scan2.xml 192.168.0.1-60
PS > $x.nmaprun.host

status address hostnames ports
------ ------- --------- -----
status {address, address} ports
status {address, address} ports


Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast (http://powerscripting.net)
Author, TechProsaic (http://halr9000.com)
mhensleyUser is Offline
New Member
New Member
Posts:14

07/17/2008 4:15 PM  

Thanks! I *love* these forums!

I was quite confused by a type name of "switch" (I would have expected "boolean"). That goes on my list of "PowerShell things that will bite you if you know other scripting/programming languages". This isn't really peculiar to PowerShell -- every new language I've ever learned led me to create such a list.

 

glnsizeUser is Offline
Shell Enthusiast
Shell Enthusiast
Posts:62


07/17/2008 4:34 PM  

@ hal
I lose focus for one moment... good catch!
Great Podcast this week… I share your security concerns with Posh via IIS.


@ mhensley
I know the feeling powershellcommunity.org, #powershell.freenode.net, and powerscripting.net... Combined the “Big Three” with the many Quality blogs, and you will seriously accelerate the learning curve.

~Glenn

 

You are not authorized to post a reply.
Forums > Using PowerShell > General PowerShell > parameter defaults



ActiveForums 3.7
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer