question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Powershell AlphaFS script stops when enumerating a file with insufficient permissions

See original GitHub issue

I’m enumerating files recursively and when a file is encountered where I don’t have access to, the scripts stops:

foreach ($file in [Alphaleonis.Win32.Filesystem.Directory]::EnumerateFiles($fullPath,'*',[System.IO.SearchOption]::AllDirectories)){ #Code }

Can result in

(5) Access is denied: [\\?\UNC\some\random\path\*] At somescript.ps1 foreach ($file in [Alphaleonis.Wi ... ~~~~~ CategoryInfo : OperationStopped: (:) [], UnauthorizedAccessException FullyQualifiedErrorId : System.UnauthorizedAccessException

I also can’t seem to trap the error with a simple try catch within the loop, as the issue is when enumerating $file. Doing it afterwards will cancel the loop anyway. Any ideas?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
elgonzocommented, Feb 22, 2018

FYI: Combining flag enum values can be made more readable by using the string parsing PS offers for enums. Something like:

$dirEnumOptions =
    [Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions] "ContinueOnException, Recursive"

That should at least help a little bit in constraining PS’s sporadic tendency for overly verbose letter salads… 😉 Anyway, later i too will have a beer (or two) in your name, Yomodo! 👍

1reaction
Yomodocommented, Feb 22, 2018

I think this is what you need:

$dirEnumOptions = [Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions]::ContinueOnException
[Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions]$dirEnumOptions = $dirEnumOptions -bor [Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions]::Recursive

foreach ($file in [Alphaleonis.Win32.Filesystem.Directory]::EnumerateFiles($fullPath, $dirEnumOptions)) { 
    write-host $file
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing permissions when executing powershell script
1 Answer 1 · The OP's problem turned out the inability to delete files, due to permission problems specific to their environment. ·...
Read more >
AlphaFS/samples/PowerShell/Enumerate- ...
A powerful folder/file enumerator which can report and recover from access denied exceptions and supports custom filtering. .EXAMPLE. PS C:\> .\Enumerate- ...
Read more >
Powershell script with elevated permissions still has ...
i have actually had this running before like in this question but for some reason, I am getting the message "running with full...
Read more >
MS removes 260 character path limit on NTFS in newest ...
Can't do that when the file/folder is already over the limit (unless you want to rename parent folders, which is tedious). Upvote 1...
Read more >
Bill Long's Exchange Blog - RSSing.com
The script accesses the public folder via EWS, so you must have client permissions to the folder in order for this to work...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found