Get-Childitem exits with "Access is Denied" despite -ea 0

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 2 years, 2 months ago.

  • Author
    Posts
  • #19409
    Profile photo of Kieran Walsh
    Kieran Walsh
    Participant

    Hi there,
    I'm running this line to search all ISO files on all drives:
    (GET-WMIOBJECT -query "SELECT * from win32_logicaldisk where DriveType = '3'").DeviceID| %{Get-ChildItem -path $_ -Include *.ISO -Recurse -Force -ea 0 |Select-Object FullName}

    It runs through some drives and list the ISOs that it finds along the way but then exits with this error:

    Get-ChildItem : Access is denied
    At line:1 char:92
    + ... ").DeviceID| %{Get-ChildItem -path $_ -Include *.ISO -Recurse -Force -ea 0 |Sele ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand

    If I expand the error:

    PS C:\> $error[0]|format-list -force

    Exception : System.UnauthorizedAccessException: Access to the path 'P:\System Volume Information' is
    denied.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileSystemEnumerableIterator`1.CommonInit()
    at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath,
    String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean
    checkHost)
    at System.IO.FileSystemEnumerableFactory.CreateDirectoryInfoIterator(String path, String
    originalUserPath, String searchPattern, SearchOption searchOption)
    at Microsoft.PowerShell.Commands.FileSystemProvider.Dir(DirectoryInfo directory, Boolean
    recurse, Boolean nameOnly, ReturnContainers returnContainers)
    TargetObject : P:\System Volume Information
    CategoryInfo : PermissionDenied: (P:\System Volume Information:String) [Get-ChildItem],
    UnauthorizedAccessException
    FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
    ErrorDetails :
    InvocationInfo : System.Management.Automation.InvocationInfo
    ScriptStackTrace : at , : line 1
    at , : line 1
    PipelineIterationInfo : {0, 1, 0}
    PSMessageDetails :

    I can see that I'm not allowed access to "P:\System Volume Information" which I'd expect, but I also expect the ErrorAction of SilentlyContinue to sail past this problem. Any idea why this happens?
    Thanks
    Kieran.

  • #19412
    Profile photo of Don Jones
    Don Jones
    Keymaster

    So, first of all, please use "-EA SilentlyContinue" not "-EA 0." The whole point of the enumeration is to make your code easier to read ;).

    Second, -EA only controls what will happen in the event of a non-terminating error. Terminating errors always bail, and you can't stop it, but you can catch it in a Try/Catch construct. In this case, the cmdlet is throwing a terminating error, I'm guessing. That's under the control of whoever wrote the cmdlet and the underlying technology – you can't change that.

  • #19415
    Profile photo of Kieran Walsh
    Kieran Walsh
    Participant

    Great stuff Don, didn't know about the terminating error.
    Thanks for your help!
    Kieran.

  • #19417
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Interesting if I run your line on a computer with WMF 5.0 (September Preview) it works as you've expected earlier. Looks like the PowerShell team has changed the behaviour Get-Childitem cmdlet in combination with the -Force switch.

    I agree with Don, until we can get our hands on the final of WMF 5.0 bits you'll need to use the Try/Catch construct.

    #Daniel

You must be logged in to reply to this topic.