PowerShell script - marking a parameter as required

param(
    [string] $optionalparam1, #an optional parameter with no default value
    [string] $optionalparam2 = "default", #an optional parameter with a default value
    [string] $requiredparam = $(throw ""requiredparam required."), #throw exception if no value provided
    [string] $user = $(Read-Host -prompt "User"), #prompt user for value if none provided
    [switch] $switchparam; #an optional "switch parameter" (ie, a flag)
    )

Technorati Tags:

Print | posted on Friday, December 14, 2007 12:57 PM

Feedback

# re: PowerShell script - marking a parameter as required

Left by Lance at 11/5/2008 3:11 PM
Gravatar You can also do conditional defaults. For example:

[string] $myparam = $( if($thisinput -eq "somevalue") { "defaultvalue1" } else { "defaultvalue2" } )

# re: PowerShell script - marking a parameter as required

Left by Lance at 11/5/2008 3:17 PM
Gravatar that last comment - whats the use? Well, with that method myparam's value can come from the cmd line. But if it DOESN'T, it can be autogenerated based on some other parameter value.

# re: PowerShell script - marking a parameter as required

Left by Keith at 4/9/2009 3:28 PM
Gravatar The use is, he is demonstrating that you can do conditional checking of data as another possibility...or in general run code against the inputvalue.

So take that a bit farther and imagine your evaluating if the parameter is in a valid range or format, if it is, keep the current inpout, if it is not, throw an exception or something else.

# re: PowerShell script - marking a parameter as required

Left by dave at 9/4/2009 1:55 AM
Gravatar Odd I copied and pasted your code and get the error "The left hand side of an assignment operator needs to be something that can be assigned"

# re: PowerShell script - marking a parameter as required

Left by Martin at 12/3/2009 6:43 PM
Gravatar You can also use the parameter attribute to mark a parameter as required.

param
(
[parameter(Mandatory = $true)]
[string]$requiredParam
)

If no value is provided the user will be asked to enter one.

# re: PowerShell script - marking a parameter as required

Left by Jim at 12/8/2009 1:02 AM
Gravatar This was very useful. Is there any way you can specify a parameter based on the value of another parameter?

I have a powershell script that has a parameter of -sendMail. This is set as a switch, so the default is $false. What I'd like is to have a mandatory parameter that must have a value if -sendMail is specified from the command line (so it's set to $true). I have two possible email formats, one for staff and one for pupils and I'd like a prompt to enter which one if -sendMail is used as a paramter. Hope that makes sense?

# re: PowerShell script - marking a parameter as required

Left by Lance at 12/8/2009 1:41 AM
Gravatar Yep, you can put pretty much any expression in the default evaluator. For example:

param(
[switch] $switchparam, #an optional "switch parameter" (ie, a flag)
[string] $user = $(if ($switchparam) { Read-Host -prompt "User"}) #prompt user for value if none provided
)

# re: PowerShell script - marking a parameter as required

Left by Jim at 12/8/2009 2:14 AM
Gravatar That's exactly what I wanted. Thank you very much!

# re: PowerShell script - marking a parameter as required

Left by Justin Dearing at 3/26/2011 2:17 PM
Gravatar Thanks for writing this article. Also, the comments give some great ideas for expanding for this. You have won a new twitter follower as well as a new google reader subscriber.

# re: PowerShell script - marking a parameter as required

Left by Justin Dearing at 3/26/2011 2:50 PM
Gravatar Perhaps it would be better practice to use an ArgumentException:

param(
[string] $ObjectName = $(Throw New-Object System.ArgumentException "Parameter -ObjectName must be set to the name of a database object","ObjectNamt")
);

I'm new to Powershell, and learning that my C# assumptions do not always translate to Powershell very well. Therefore I decided to put the matter up for debate on stackoverflow: http://stackoverflow.com/questions/5444466/any-reason-not-to-throw-an-argumentexception-in-a-powershell-param-block

# re: PowerShell script - marking a parameter as required

Left by Lance at 3/31/2011 9:44 AM
Gravatar Hey thanks for the follows Justin - and I think thats a fine suggestion. I think all three methods work just fine.

Your comment:





 
 

Copyright © Lance Robinson

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski