I'm writing a custom PowerShell cmdlet, and I would like to know which is the proper way to validate a parameter.
I thought that this could be done either in the property set accessor or during Cmdlet execution:
[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
}
}
}
Which is the "standard" approach? Property setter, ProcessRecord or something completely different?