header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
how to specify parameter is NULL by default in Powershell
Last Post 26 Nov 2010 09:55 PM by Bartek Bielawski. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
BikeBoyUser is Offline
New Member
New Member
Posts:25
Avatar

--
24 Nov 2010 11:09 AM
    Hi all,
    How can I easily specify that if the parameter is not passed, it should be NULL by default. So in this example $Var1 should always be passed, and I check for that. $Var2 most of the time WILL NOT be passed at all, and it's ok. Instead of doing the following, I need to set $Var2 to default of NULL unless it's passed to the script.

    $Var1=$args[0]
    $Var2=$args[1]
    if (!$args[0]) { write-host "Invalid or missing sql server (Argument 1)" exit }

    # If Var2 is not provided it's NULL

    if (!$args[1]) {$var -eq $null}

    ------------
    Thanks
    Bartek BielawskiUser is Offline
    New Member
    New Member
    Posts:43
    Avatar

    --
    24 Nov 2010 12:08 PM
    I would use param () instead, version that should work on v1 and v2:
    param (
    $var1 = $(throw 'I really need var1!'),
    $var2
    )

    v2 version:

    [CmdletBinding()]
    param (
    [Parameter(Mandatory = $true, HelpMessage = 'Please provide the script with first argument')]
    $var1,
    $var2
    )

    It will force user of the script to pass first argument, and var2 will contain $null if not passed.

    HTH
    Bartek
    BikeBoyUser is Offline
    New Member
    New Member
    Posts:25
    Avatar

    --
    26 Nov 2010 12:17 PM
    Thanks Bartek,
    I like the [CmdletBinding()] to make $Var1 mandatory, but since my script is kicked off by scheduling engine, it'll just sit there waiting for $var1 forever, so that's why I need to check for it manually, or exit.


    Just curious, how is param () better than $args[0] syntax ? Is that because it's more flexable as a function?

    Bartek BielawskiUser is Offline
    New Member
    New Member
    Posts:43
    Avatar

    --
    26 Nov 2010 09:55 PM
    Well, I would expect ValidateNotNullOrEmpty would do the job in such a case, but it kick in only if you run function with empty string or $null. :(
    In such a case:
    param (
    [Parameter()]
    [ValidateNotNullOrEmpty()]
    $var1 = $(throw "I really need a value for Var1!")

    Setting default value in that way was v1 way to make parameter mandatory, but it does not prompt the user for it. ;)

    Why param? This way you have more control over parameters being passed to your function. And if user will try to pass more arguments than you expected, it will throw an error too. ;) Of course once you do [CmdletBinding()].
    But you can't use CmdletBinding without PARAM().
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer