Home > Enterprise >  How do I use validatepattern for multiple integers?
How do I use validatepattern for multiple integers?

Time:11-29

Basically, I need to use validate pattern to validate a few numbers. I need to be able to run .\Myfilename "number" "number" "number" "number" "number" and it should do a few things with those numbers in the script.

An actually example of what I would type is: .\Myfilename 100 100 20 30 50

What I can't figure out is how to use validatepattern to assure these numbers are integers.

This is what I tried:

Param ([ValidatePattern("\d")] $G1 = 0, $G2= 0, $G3 = 0, $G4 = 0, $G5 = 0)

I also tried:

Param ([ValidatePattern("\d\s\d\s\d\s\d\s\d")] $G1 = 0, $G2= 0, $G3 = 0, $G4 = 0, $G5 = 0)

To no avail... Any help would be appreciated!

CodePudding user response:

I don't believe a ValidatePattern Attribute is needed in this case, unless there actually is a specific pattern you want the arguments to have. In addition, you don't need to have one parameter per argument. You can have only one parameter and type constraint it to [int[]] and have the parameter as ValueFromRemainingArguments argument to make sure you're capturing all inputs.

  • Sample script.ps1:
[cmdletbinding()]
param(
    [parameter(ValueFromRemainingArguments)]
    [int[]]$Numbers
)

$i = 0

foreach($number in $Numbers)
{
    [pscustomobject]@{
        ArgumentNumber = ($i  )
        Input = $Number
        IsInt = $Number -is [int]
    }
}
  • Sample Input and Output:
PS /> ./script.ps1 123 345 567           

ArgumentNumber Input IsInt
-------------- ----- -----
             0   123  True
             1   345  True
             2   567  True

And if you were to use an invalid input such as a string:

PS /> ./script.ps1 123 345 567 'asd'
  • $Error[0].Exception.InnerException.Message would be:

Cannot convert value "System.Collections.Generic.List`1[System.Object]" to type "System.Int32[]". Error: "Cannot convert value "asd" to type "System.Int32". Error: "Input string was not in a correct format.""


Note: By using this method, the input should always follow one of the following formats:

  • <int><white space><int><white space><int>...., i.e.: 123 345 567
  • <int><comma><int><comma><int><comma>...., i.e.: 123, 345, 567

An input that does not follow this syntax will get you an exception.

  • <int><comma><int><white space><int>, i.e.: 123, 345 567

Will get you the following exception:

Cannot convert value "System.Collections.Generic.List`1[System.Object]" to type "System.Int32[]". Error: "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32"."


[ValidatePattern("\d")] is not a valid pattern for what you're looking for (one or more numeric digits per argument). \d will accept any input that has at least 1 numeric digit:

[cmdletbinding()]
param (
    [ValidatePattern("\d")] $Grade1 = 75
)

"Input was: $Grade1" 
PS /> ./script.ps1 1asd
Input was: 1asd

PS /> ./script.ps1 asd2
Input was: asd2

If you want the input to be all numeric digits, the easiest way is to just type constraint the parameter and if really need to use Validate Pattern, the pattern for all numeric digits would be ^\d $:

[cmdletbinding()]
param (
    [ValidatePattern("^\d $")]
    [int] $Grade1 = 75
)
  • Related