Any way to show full error message?

AZLearner 96 Reputation points
2021-07-16T23:24:14.257+00:00

Hi,

I am writing a simple PowerShell script to dump all the folders/files permissions under a top level directory via its UNC path. I notice if I don't have access to a subdirectory, it errors out and continues but the path shown in the error message is partial (missing the middle part). Is there a way to show the full path of the directory/file that I am lack of permission? For example, below it does not show the full path for "\fileserver\co...gment Reporting".

Get-ChildItem : An unexpected network error occurred.
At C:\script\dumpdirperm.ps1:10 char:15
+ ... olderPath = Get-ChildItem -Directory -Path $PATH_TOP  -Recurse -Force ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ReadError: (\\fileserver\co...gment Reporting:String) [Get-ChildItem], IOException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

Thank you.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 48,026 Reputation points
    2021-07-17T01:36:09.573+00:00

    You could try wrapping the Get-ChildItem in a try/catch:

    Try{
      Get-ChildItem . . . -ErrorAction STOP
    }
    Catch{
      # do something with the Error Object here
      # maybe something like this to get the
      # target:
      $_.TargetObject
    }
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.