Any reason not to throw an ArgumentException in the powershell params () block?

I was looking for a way to make the necessary parameters in powershell when I discovered this blog post suggesting the following:

param( [string] $ObjectName = $(Throw "Parameter -ObjectName must be set to the name of a database object") ); 

After digesting for some time, I came to the conclusion that it would be better to throw an ArgumentException as opposed to a line:

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

Now, in terms of C #, the latter will be better. Is there a reason this practice doesn't translate to powershell?

+6
powershell
source share
1 answer

In PowerShell 2.0, you can mark a parameter as Manadatory and let PowerShell do the work for you:

 Param( [Parameter(Mandatory=$true, Position=0, HelpMessage='ObjectName must be set to the name of a database object')] [string] $ObjectName ) 
+10
source share

All Articles