Get specific information of an error

This topic contains 7 replies, has 3 voices, and was last updated by  Vandrey Trindade 4 weeks ago.

  • Author
    Posts
  • #80167

    Vandrey Trindade
    Participant

    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"
    }
    
  • #80171

    Kevyn
    Participant

    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("()")
    
    • #80185

      Vandrey Trindade
      Participant

      Kevin,

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

    • #80192

      Kevyn
      Participant

      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.

    • #80216

      Vandrey Trindade
      Participant

      Thanks Kevin!

  • #80182

    Matt Bloomfield
    Participant

    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"
    }
    
    • #80188

      Vandrey Trindade
      Participant

      Matt Bloomfield,

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

    • #80191

      Matt Bloomfield
      Participant

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

You must be logged in to reply to this topic.