Get specific information of an error

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

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("()")

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

Kevin,

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

Matt Bloomfield,

That’s what I want! :smiley:
The only way to get that number is using regex?

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

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.

Thanks Kevin!