Check PowerShell cmdlet parameters

I am writing a custom PowerShell cmdlet and I would like to know what is the correct way to validate a parameter.
I thought this could be done either in the accessory of the property set, or during the execution of the cmdlet:

[Cmdlet(VerbsCommon.Add,"X")] public class AddX : Cmdlet { private string _name; [Parameter( Mandatory=false, HelpMessage="The name of the X")] public string name { get {return _name;} set { // Should the parameter be validated in the set accessor? if (_name.Contains(" ")) { // call ThrowTerminatingError } _name = value; } } protected override void ProcessRecord() { // or in the ProcessRecord method? if (_name.Contains(" ")) { // call ThrowTerminatingError } } } 

What is the "standard" approach? Property collection, ProcessRecord or something completely different?

+4
source share
1 answer

If possible, he would prefer that the parameters be checked at runtime by specifying validation attributes in the parameter definition.

Windows PowerShell can validate arguments passed to cmdlet parameters in several ways. Windows PowerShell can check the length, range, and character pattern of an argument. It can check the number of available arguments (counter).

+7
source

All Articles