Get specific information of an error

Welcome Forums General PowerShell Q&A Get specific information of an error

This topic contains 7 replies, has 3 voices, and was last updated by

 
Participant
1 year, 6 months ago.

  • Author
    Posts
  • #80167

    Participant
    Points: 45
    Rank: Member

    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

    Participant
    Points: 1
    Rank: Member

    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

      Participant
      Points: 45
      Rank: Member

      Kevin,

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

    • #80192

      Participant
      Points: 1
      Rank: Member

      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

      Participant
      Points: 45
      Rank: Member

      Thanks Kevin!

  • #80182

    Participant
    Points: 41
    Rank: Member

    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

      Participant
      Points: 45
      Rank: Member

      Matt Bloomfield,

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

    • #80191

      Participant
      Points: 41
      Rank: Member

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

The topic ‘Get specific information of an error’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort