How to check dependent parameters in PowerShell cmdlet

What is the best way to check PowerShell syntax for dependent parameters? For example, in the cmdlet example below, I need to verify that the Low value is greater than High, but this is not possible with the verification attributes.

[Cmdlet(VerbsCommon.Get, "FakeData")] public class GetFakeData : PSCmdlet { [Parameter(Mandatory = true)] [ValidateNotNullOrEmpty] public int Low { get; set; } [Parameter(Mandatory = true)] [ValidateNotNullOrEmpty] public int High { get; set; } protected override void BeginProcessing() { if (Low >= High) { // Is there a better exception to throw here? throw new CmdletInvocationException("Low must be less than High"); } base.BeginProcessing(); } protected override void OnProcessRecord() { // Do stuff... } } 

Is there a better way to do this? The main thing that I do not like about the solution above is that I cannot throw a ParameterBindingException , as the validation attributes would do, since this is an inner class. I could throw an ArgumentException or PSArgumentException , but this is valid for not cmdlets methods.

+4
source share
1 answer

You need something like the get-random cmdlet. Since you cannot use the [validatescript()] attribute in the cmdlet because it is only valid for the powershell / script function at run time, you need to steal the idea from microsoft.powershell.utility \ get-random:

Validation is done in BeginProcessing () and uses the custom error ThrowMinGreaterThanOrEqualMax

 protected override void BeginProcessing() { using (GetRandomCommand.tracer.TraceMethod()) { if (this.SetSeed.HasValue) this.Generator = new Random(this.SetSeed.Value); if (this.EffectiveParameterSet == GetRandomCommand.MyParameterSet.RandomNumber) { if (this.IsInt(this.Maximum) && this.IsInt(this.Minimum)) { int minValue = this.ConvertToInt(this.Minimum, 0); int maxValue = this.ConvertToInt(this.Maximum, int.MaxValue); if (minValue >= maxValue) this.ThrowMinGreaterThanOrEqualMax((object) minValue, (object) maxValue); this.WriteObject((object) this.Generator.Next(minValue, maxValue)); } else { double min = this.ConvertToDouble(this.Minimum, 0.0); double max = this.ConvertToDouble(this.Maximum, double.MaxValue); if (min >= max) this.ThrowMinGreaterThanOrEqualMax((object) min, (object) max); this.WriteObject((object) this.GetRandomDouble(min, max)); } } else { if (this.EffectiveParameterSet != GetRandomCommand.MyParameterSet.RandomListItem) return; this.chosenListItems = new List<object>(); this.numberOfProcessedListItems = 0; if (this.Count != 0) return; this.Count = 1; } } } 

...

 private void ThrowMinGreaterThanOrEqualMax(object min, object max) { if (min == null) throw GetRandomCommand.tracer.NewArgumentNullException("min"); if (max == null) throw GetRandomCommand.tracer.NewArgumentNullException("max"); string errorId = "MinGreaterThanOrEqualMax"; this.ThrowTerminatingError(new ErrorRecord((Exception) new ArgumentException(this.GetErrorDetails(errorId, min, max).Message), errorId, ErrorCategory.InvalidArgument, (object) null)); } 

You can use the decompiler ( dotPeak ) to see the rest of the code to learn more about the custom error for cmdlet

+2
source

Source: https://habr.com/ru/post/1501753/


All Articles