1

Any help would be greatly appreciated. Essentially, this code takes directories and subdirectories and enumerates through each file and pulls properties for each of those files and returns all of that information. It compiles this C# code in PowerShell and then utilizes the SubDirectories() method. The code works absolutely fine until I use it on a file share that contains folders that I do not have access to(elevating permissions to Administrator will not be an option here). In the current code, my catch statement accounts for the UnauthorizedAccessException, yet it terminates because of this exception. I am looking for a fix that will allow the code to recognize the UnauthorizedAccessException and then simply move on to the next folder. Does anybody know how I would go about doing that? Thanks a bunch in advance.

$file_share = @"
using System;
using System.IO;
using System.Collections.Generic;
using System.Security.AccessControl;

namespace FileShareWork
{
    public class Program
    {

       
        public static void Main(string[] args)
        {
            SubDirectories();
         
        }

        public static void SubDirectories()
        {
    
            
            string sourceDirectory = @"Z:\";
                   
            DirectoryInfo currentParentDirectory = new DirectoryInfo(sourceDirectory);
            DirectoryInfo[] listOfSubDirectories = currentParentDirectory.GetDirectories();
            List<string>convertPaths = new List<string>();
            List<string>filesPerPath = new List<string>();
            List<string>fullFileDetails = new List<string>();
              
     
            foreach (DirectoryInfo info in listOfSubDirectories)
            {
               
              
                convertPaths.Add(info.FullName.ToString());
         
                
            }
                    
         
           for(int counter = 0; counter <= convertPaths.Count; counter++)
           {

               
                    string subPath = convertPaths[counter];
                    DirectoryInfo individualSubfolder = new DirectoryInfo(subPath);
                   
                      try
                      {
                        
                        foreach(var eachFile in individualSubfolder.EnumerateFiles("*", SearchOption.AllDirectories))
                        {
                            string fileOwner = System.IO.File.GetAccessControl(eachFile.FullName).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
                            string individualPaths = eachFile.FullName.ToString();
                            string creationTime = eachFile.CreationTime.ToString();
                            string lastWriteTime = eachFile.LastWriteTime.ToString();
                            string lastAccessed = eachFile.LastAccessTime.ToString();
                            string fileSize = eachFile.Length.ToString();
                            string fileDirectory = eachFile.Directory.ToString();
                            filesPerPath.Add(eachFile.FullName.ToString());
                            fullFileDetails.Add(individualPaths);
                            fullFileDetails.Add(creationTime);
                            fullFileDetails.Add(lastWriteTime);
                            fullFileDetails.Add(lastAccessed);
                            fullFileDetails.Add(fileSize);
                            fullFileDetails.Add(fileDirectory);
                            fullFileDetails.Add(fileOwner);
                                             
                        }

                      }

                      catch (UnauthorizedAccessException){}

                 
                    counter++;
                                          
           }
         
         fullFileDetails.ForEach(i => Console.WriteLine(i));  
        }
     
    }
}

"@


Set-Alias new New-Object

Add-Type -TypeDefinition $file_share -Language CSharp

Remove-Variable $file_share

cls

[FileShareWork.Program]::SubDirectories()
mklement0
  • 382,024
  • 64
  • 607
  • 775
Athanasius12
  • 37
  • 1
  • 6
  • I fear you'll have to create a .NET _Core_ assembly if you want to ignore inaccessible directories - see [this answer](https://stackoverflow.com/q/54219607/45375) to the linked dupicate. – mklement0 Nov 23 '20 at 19:58
  • 1
    I appreciate it. – Athanasius12 Nov 23 '20 at 20:00
  • What the reason for doing it this way to begin with? Why not just use PowerShell? – PMental Nov 23 '20 at 20:01
  • Partly, I am just getting some C# practice in. Plus, there are things that can be done more quickly/robustly with C# than can be done with a PowerShell Cmdlet. Also, this code is just a framework for something bigger. – Athanasius12 Nov 23 '20 at 20:08

0 Answers0