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

function param and default value
Last Post 13 Aug 2008 09:22 PM by Elan. 9 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
ElanUser is Offline
New Member
New Member
Posts:28

--
11 Aug 2008 10:33 PM  

Just started scripting recently and the following has been bugging me:

Script File Name C:\Add.ps1

Function Add
{
    Param ([int]$x = 2, [int]$y = 4)

    $Ans = $x + $y
     Write-Host $Ans
}

Add $args[ 0 ] $args[ 1 ]

 

When I call the script using .\Add.ps1 2 6 it'll add them up just fine. 

But If I leave the 2 and 6 out such that it looks like ".\Add.ps1" and that's all, the defaults of x being 2 and y being 4 won't work and I get blank output.  Would anybody know why when using $args[ 0 ] it causes Param[int]$x = 2  to not work and the script returns no information?

Thanks.

smurawskiUser is Offline
New Member
New Member
Posts:46

--
11 Aug 2008 10:54 PM  
My guess would be that when you specificy a parameter, even if that value is null, it will not use the "default" value.

When you call add with $args[ 0 ] and $args[ 1 ], it is calling add and assigning $null (which is evaluated as 0 when cast as an int) to both $x and $y.

The result of calling add with two null values is 0 + 0.
ElanUser is Offline
New Member
New Member
Posts:28

--
11 Aug 2008 11:53 PM  
If it was really 0 + 0 then wouldn't the result show up as 0? It doesn't show anything.
smurawskiUser is Offline
New Member
New Member
Posts:46

--
12 Aug 2008 12:10 AM  
I do get 0 when I run it. You are getting no output?
ElanUser is Offline
New Member
New Member
Posts:28

--
12 Aug 2008 12:16 AM  

Oops, I am getting 0.  It's when I try it on my other script it doesn't return anything because my real script is doing this with strings instead of integers.

smurawskiUser is Offline
New Member
New Member
Posts:46

--
12 Aug 2008 01:31 AM  
No problem. In the case of strings, $null gets cast to "" which is an empty string, which could look like no result.
ElanUser is Offline
New Member
New Member
Posts:28

--
12 Aug 2008 02:34 AM  
Appreciate the help. Assigning a default value to a function seems to be useless then if you can't use $args to pass a blank value to the function allowing it to use the default value.
halr9000User is Offline
Basic Member
Basic Member
Posts:316

--
12 Aug 2008 02:50 AM  
Honestly, I would avoid doing it this way. My suggestions, in no particular order:

- Don't use $args--it's so easy to do named params in powershell which are vastly superior
- Remember GIGO. If you pass $null's into the add function, then $null is what you get out!
- Why not move your entire Params() block to the first line of the script (and remove the last line)? That will make it behave as you think it would.
- To make it work like you want, with the logic exactly as you want, then you would need to perform tests to see if there are $args and if so, do it this way and if not, call the function w/o any params, which would allow those defaults to kick in. e.g.

if ( $args ) { add $args[ 0 ] $args [ 1 ] }
else { add }


But again--I wouldn't use $args because I've totally written them off. :)

P.S., the forum software doesn't like bracket-number-bracket for some reason. Inserting a space is a workaround until we get that fixed.
halr9000User is Offline
Basic Member
Basic Member
Posts:316

--
12 Aug 2008 02:52 AM  
Or even
if ( $args.length -eq 2 ) { add $args [ 0 ] $args[ 1 ] }
ElanUser is Offline
New Member
New Member
Posts:28

--
13 Aug 2008 09:22 PM  
Thanks a ton hair9000. That has helped me out greatly!
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer