Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I use validatepattern for multiple integers?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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.""


Side note, using this method the input should always follow one of the following formats:

  • <int><white space><int><white space><int><white space>
  • <int><comma><int><comma><int><comma>

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"."

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading