Geeks With Blogs

Lance's TextBox » About Me » My Resume »Twitter

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:
Posted on Friday, December 14, 2007 12:57 PM PowerShell | Back to top


Comments on this post: PowerShell script - marking a parameter as required

# re: PowerShell script - marking a parameter as required
Requesting Gravatar...
You can also do conditional defaults. For example:

[string] $myparam = $( if($thisinput -eq "somevalue") { "defaultvalue1" } else { "defaultvalue2" } )
Left by Lance on Nov 05, 2008 3:11 PM

# re: PowerShell script - marking a parameter as required
Requesting 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.
Left by Lance on Nov 05, 2008 3:17 PM

# re: PowerShell script - marking a parameter as required
Requesting 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.

Left by Keith on Apr 09, 2009 3:28 PM

# re: PowerShell script - marking a parameter as required
Requesting 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"
Left by dave on Sep 04, 2009 1:55 AM

# re: PowerShell script - marking a parameter as required
Requesting 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.
Left by Martin on Dec 03, 2009 6:43 PM

# re: PowerShell script - marking a parameter as required
Requesting 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?
Left by Jim on Dec 08, 2009 1:02 AM

# re: PowerShell script - marking a parameter as required
Requesting 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
)

Left by Lance on Dec 08, 2009 1:41 AM

# re: PowerShell script - marking a parameter as required
Requesting Gravatar...
That's exactly what I wanted. Thank you very much!
Left by Jim on Dec 08, 2009 2:14 AM

# re: PowerShell script - marking a parameter as required
Requesting 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.
Left by Justin Dearing on Mar 26, 2011 2:17 PM

# re: PowerShell script - marking a parameter as required
Requesting 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
Left by Justin Dearing on Mar 26, 2011 2:50 PM

# re: PowerShell script - marking a parameter as required
Requesting Gravatar...
Hey thanks for the follows Justin - and I think thats a fine suggestion. I think all three methods work just fine.
Left by Lance on Mar 31, 2011 9:44 AM

Your comment:
 (will show your gravatar)


Copyright © Lance Robinson | Powered by: GeeksWithBlogs.net