none
Catching an error.... TypeNoteFound RRS feed

  • Question

  • Hello -

    First my PowerShell verion:

    PS C:\windows\system32> $PSVersionTable.PSVersion

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    5      0      10586  1176  


    I am trying to catch the following output:

    Compare-Object : Cannot bind argument to parameter 'ReferenceObject' because it is null.
    At line:62 char:43
    +                 $Result = Compare-Object  $SrcDir $DstDir -Property H ...
    +                                           ~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Compare-Object], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand

    I am using the following:

            try {
                    # code that causes exception
                    $Result = Compare-Object  $SrcDir $DstDir -Property Hash,Path | leftside | Format-Table Path -ErrorAction Continue
    
                }
           catch [System.Management.Automation.ParameterArgumentValidationErrorNullNotAllowed]   {
                          
                    #Write-Host "An error occurred: $($error[0].errordetails.message)" }
                 
                

    I am receiving the following:

    Unable to find type [System.Management.Automation.ParameterArgumentValidationErrorNullNotAllowed].
    At line:53 char:17
    + ...             $Result = Compare-Object  $SrcDir $DstDir -Property Hash, ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Manageme...rNullNotAllowed:TypeName) [], RuntimeException
        + FullyQualifiedErrorId : TypeNotFound

    I used to grab the ParameterArgumentValidationErrorNullNotAllow part.

            try {
                    # code that causes exception
                    $Result = Compare-Object  $SrcDir $DstDir -Property Hash,Path | leftside | Format-Table Path -ErrorAction Continue
    
                }
           catch    {
                    $_.Exception.GetType().FullName
                    
                    }

    Any suggestions.... I don't have much more hair left to lose.  Thanks.

    The Compare-Object is being used to compere MD5 Hash on files and directories.





    • Edited by jasonh17 Thursday, November 9, 2017 9:27 PM
    • Moved by Bill_Stewart Thursday, January 25, 2018 10:15 PM Unanswerable drive-by question
    Thursday, November 9, 2017 9:23 PM

All replies

  • Many types cannot be directly caught that way.


    \_(ツ)_/

    Thursday, November 9, 2017 11:30 PM
  • The exception is telling your that $SrcDir is null.

    \_(ツ)_/

    Thursday, November 9, 2017 11:35 PM
  • Here is how to trap parameter errors>

    Try{
        Compare-Object  $SrcDir $DstDir -ErrorAction Stop
    }
    Catch [System.Management.Automation.ParameterBindingException] {
        Write-Host $_
    }


    \_(ツ)_/

    Thursday, November 9, 2017 11:39 PM