locked
get-childitem aborted after access denied RRS feed

  • Question

  • Try to use below with remove-items to clean up the logs but the code stopped after access denied error.  I tried different erroraction including continue, ignore, silentlycontinue and $erroractionpreference, same result.   Can someone tell me how to get gci process the entire logs folder?   thanks

    gci 'D:\Logs' -Include '*.log', '*.bak', '*.etl' -Recurse -erroraction SilentlyContinue  | ? LastWriteTime -lt (Get-Date).AddDays(-5)

    gci : Access is denied
    At line:1 char:1
    + gci 'D:\Logs' -Include '*.log','*.blg', '*.bak', '*.etl' -Recur ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
        + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand

    Saturday, May 5, 2018 5:52 PM

All replies

  • You will have to be an Admin to access that folder.


    \_(ツ)_/

    Saturday, May 5, 2018 7:08 PM
  • You will have to be an Admin to access that folder.


    \_(ツ)_/


    I tried run PS as administrator but same result.  My issue is not the "access denied error, I just want to skip the files with access denied or file locked error and continue the next file/folder.  The "erroraction" doesn't work as I expect, the command also stopped after the first access denied exception.
    Saturday, May 5, 2018 8:35 PM
  • The "access denied" is on the root folder so there are no other folders and files to get.  Up must have access to the root.


    \_(ツ)_/

    Saturday, May 5, 2018 8:37 PM
  • The "access denied" is on the root folder so there are no other folders and files to get.  Up must have access to the root.


    \_(ツ)_/

    My fault, I modified the output.  The error is on a subfolder of d:\logs, it processed thousands of files but stopped on the first access denied exception.   So why does the command stop after first exception when erroraction is not "stop"? 


    • Edited by AndyHWC Saturday, May 5, 2018 9:06 PM
    Saturday, May 5, 2018 9:06 PM
  • It doesn't quit on an access denied on a subfolder or file.

    The correct command syntax is:

    Get-ChildItem D:\Logs\* -Include *.log,*.bak,*.etl -Recurse -erroraction SilentlyContinue 


    \_(ツ)_/

    Saturday, May 5, 2018 9:11 PM
  • It doesn't quit on an access denied on a subfolder or file.

    The correct command syntax is:

    Get-ChildItem D:\Logs\* -Include *.log,*.bak,*.etl -Recurse -erroraction SilentlyContinue 


    \_(ツ)_/


    That's the same as mine except I pipe it to " ? LastWriteTime -lt (Get-Date).AddDays(-5)"

    Without the filter or using your command.  The command stopped at exact same place - the first file that caused the access denied exception. 
    Saturday, May 5, 2018 9:23 PM
  • Then there is something wrong with your system.

    Are you using a very old version of PS?


    \_(ツ)_/

    Saturday, May 5, 2018 11:04 PM

  • That's the same as mine

    No it isn't It has a significant difference.

    Remove the erroraction and see what happens.


    \_(ツ)_/

    Saturday, May 5, 2018 11:06 PM
  • Then there is something wrong with your system.

    Are you using a very old version of PS?


    \_(ツ)_/

    I added the erroraction because it didn't work with default exception handing (I believe default is 'continue').  I tried your command without erroraction and same result. 

    PS is 4.0.  It's a Windows 2012 server with Exchange 2016.

    Name                           Value
    ----                           -----
    PSVersion                      4.0
    WSManStackVersion              3.0
    SerializationVersion           1.1.0.1
    CLRVersion                     4.0.30319.42000
    BuildVersion                   6.3.9600.18773
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
    PSRemotingProtocolVersion      2.2

    Saturday, May 5, 2018 11:24 PM
  • I can run that on any folder and only get access denied on files and folders I have no access to.  It doesn't stop when an error occurs.

    This is how PS has always worked for me and everyone else. Also you error message is of as it says you have no access to the GCI command.  This is the normal error.

    PS D:\scripts> gci c:\windows\* -Include *.log -Recurse
    gci : Access to the path 'C:\windows\appcompat\Programs' is denied.
    At line:1 char:1
    + gci c:\windows\* -Include *.log -Recurse
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : PermissionDenied: (C:\windows\appcompat\Programs:String) [Get-ChildItem], UnauthorizedAccessException
        + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand


    \_(ツ)_/

    Sunday, May 6, 2018 12:22 AM