How do I catch this?

This topic contains 5 replies, has 4 voices, and was last updated by  Martin Nielsen 3 years, 3 months ago.

  • Author
    Posts
  • #15886

    Tony Wainwright
    Participant

    I'm trying to modify a script that check the size of folders in a particular path, but get an error message when it hits a folder of zero bytes. Is there a way to use a Try Catch to suppress the error message?

    Code snippet
    $size = (Get-ChildItem $_.FullName -Recurse |
    WHERE {-not $_PSIsContainer} |
    Measure-Object -Sum Length).Sum

    Error Message
    Measure-Object : The property "Length" cannot be found in the input for any objects.
    At line:64 char:13
    + Measure-Object -Sum Length).Sum
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand

    Thanks
    Tony

  • #15887

    Don Jones
    Keymaster

    You've got a typo. You're missing a period after the $_. That's why it's including folders.

  • #15888

    Tony Wainwright
    Participant

    Thanks Don...it was actually a huge typo. The error I'm trying to catch is permission denied:
    Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\WMI\RtBackup' is denied.
    At T:\Scripts\Folders\Get-DirSize.ps1:62 char:18
    + $size = (Get-ChildItem $_.FullName -Recurse |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (C:\Windows\Syst...es\WMI\RtBackup:String) [Get-ChildItem], Unaut
    horizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

  • #15889

    Dave Wyatt
    Moderator

    That's a non-terminating error. I would tend to use a combination of -ErrorAction SilentlyContinue (to keep the red text off the screen) and -ErrorVariable (to allow your script to handle the error later):

    $size = (Get-ChildItem $_.FullName -Recurse -ErrorAction SilentlyContinue -ErrorVariable gciErrors |
                WHERE {-not $_.PSIsContainer} |
                Measure-Object -Sum Length).Sum
    
    foreach ($errorRecord in $gciErrors)
    {
        # Do something with the error record.
    }
    
  • #15890

    Tony Wainwright
    Participant

    Thanks, worked a treat.

  • #15897

    Martin Nielsen
    Participant

    In your case it might not be important to handle the error right away, but cases where it is you can explicitly tell the Cmdlet to throw a terminating Exception and trigger a try/catch by using -ErrorAction Stop, or by modifying the global $ErrorActionPreference variable.


    try {
    Get-ChildItem 'C:\' -Recurse -ErrorAction Stop
    }
    catch [System.UnauthorizedAccessException] {
    "Unauthorized Access Exception"
    }
    catch {
    "Unhandled Exception"
    }

    If you want to handle specific errors, you can always look at the red error output to see what kind of Exception you're dealing with, for example:


    Get-ChildItem : Access to the path 'C:\Users\Public\Documents\Hyper-V' is denied.
    At line:1 char:1
    + Get-ChildItem C:\ -Recurse
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (C:\Users\Public\Documents\Hyper-V:String) [Get-ChildItem], UnauthorizedAccessException ## This is your Exception type
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

You must be logged in to reply to this topic.