Author Posts

September 19, 2017 at 8:11 pm

Hi,

In the code below the error message says that we gave 8 parameters and not 5.

function test {
    param (
        [Parameter(Mandatory=$true)]
        [ValidateCount(1,5)]
        [String[]]
        $ComputerName
    )
}

test -ComputerName 1,2,3,4,5,6,7,8

Using Try-Catch, is there a way to extract the number of parameters given by the user using the error information variable?

try {
    test -ComputerName 1,2,3,4,5,6,7,8
}
catch [System.Management.Automation.ValidationMetadataException] {
    Write-Warning "Error in the quantity of parameters given. You gave: (???) parameters"
}

September 19, 2017 at 8:48 pm

Just by running the "test" function as you have it, you get the below error:

test : Cannot validate argument on parameter 'ComputerName'. The number of provided arguments, (8), exceeds the maximum number of allowed arguments (5). Provide fewer than 5 
arguments, and then try the command again.
At line:10 char:20
+ test -ComputerName 1,2,3,4,5,6,7,8
+                    ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [test], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,test

It looks like you're planning to call your "test" function inside of a script or function. You can do the following. You'll need to turn the script/function that's got the below Try/Catch blocks in by adding [CmdletBinding()] at the top.

Try
{
     test -ComputerName 1,2,3,4,5,6,7,8 -ErrorAction Stop
}
Catch [System.Management.Automation.ValidationMetadataException]
{
    Write-Warning "$($_.Exception.Message)"
}

You get:

WARNING: Cannot validate argument on parameter 'ComputerName'. The number of provided arguments, (8), exceeds the maximum number of allowed arguments (5). Provide fewer than 5 arguments, and then try the command again.

Personally, I like the above details, but if you really want to write you own warning details, you can use the -split operator to split exception message on the comma (,), grab the right string object, & trim the space and parenthesis off to get the number 8.

$ComputerNameQuantitySupplied = ($($_.Exception.Message -split ",")[1].Trim()).Trim("()")

September 19, 2017 at 9:04 pm

Kevin beat me to it. I was going to say pretty much the same thing regarding the Exception.Message text. However, I used a different technique to extract the number from the message for a custom error:

try {
    test -ComputerName 1,2,3,4,5,6,7,8
}
catch [System.Management.Automation.ValidationMetadataException] {
    $pCount = [regex]::Match($Error[0].Exception.Message,"\d+").value
    Write-Warning "You gave $pCount parameters"
}

September 19, 2017 at 9:14 pm

Kevin,

Thanks for replying.
I just wanted that 8 lol
Is there any property of $Error that can be used?

September 19, 2017 at 9:15 pm

Matt Bloomfield,

That's what I want! 😀
The only way to get that number is using regex?

September 19, 2017 at 9:28 pm

I think so, I can't see any property in the exception that's thrown that contains the value.

September 19, 2017 at 10:47 pm

The split & trimming that I'm doing does get you the "8". It's just a different way of getting it than what Matt did. So, RegEx isn't the only way to get the value.