Advertisement
Guest User

Untitled

a guest
Feb 7th, 2024
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Define the drive or folder to scan
  2. $rootPath = "C:\" # Adjust to your target drive or folder
  3.  
  4. # Define the base path for output CSV files
  5. $baseOutputCsv = "C:\temp\fileDetails" # Base path and name for output files
  6.  
  7. # Initialize the file counter and line counter
  8. $fileCounter = 1
  9. $lineCounter = 0
  10. $linesPerFile = 5000 # Define how many lines you want per file
  11.  
  12. function New-OutputCsv {
  13.     param (
  14.         [string]$basePath,
  15.         [ref]$counter
  16.     )
  17.  
  18.     $fileName = "${basePath}_$($counter.Value).csv"
  19.     "Path,Size (KB),LastAccessTime,LastWriteTime,CreationTime,Permissions" | Out-File $fileName
  20.     $counter.Value++
  21.     $global:lineCounter = 0 # Reset line counter for the new file
  22.     return $fileName
  23. }
  24.  
  25. function Add-Line {
  26.     param (
  27.         [string]$line,
  28.         [string]$outputCsv
  29.     )
  30.    
  31.     Add-Content -Path $outputCsv -Value $line
  32.     $global:lineCounter++
  33.    
  34.     # Check if we need to split into a new file based on line count
  35.     if ($global:lineCounter -ge $global:linesPerFile) {
  36.         $global:outputCsv = New-OutputCsv -basePath $baseOutputCsv -counter ([ref]$fileCounter)
  37.     }
  38. }
  39.  
  40. function Get-PermissionsSummary {
  41.     param (
  42.         [System.Security.AccessControl.FileSystemSecurity]$Acl
  43.     )
  44.     $permissions = @()
  45.     foreach ($access in $Acl.Access) {
  46.         # Correct usage of variable inside a string
  47.         $identity = $access.IdentityReference.Value
  48.         $rights = $access.FileSystemRights
  49.         $accessType = $access.AccessControlType
  50.         $permissions += "${identity}: $rights ($accessType)"
  51.     }
  52.     return $permissions -join "; "
  53. }
  54.  
  55.  
  56. function Safe-EnumerateFiles {
  57.     param (
  58.         [System.IO.DirectoryInfo]$Directory
  59.     )
  60.    
  61.     # Handle directories
  62.     try {
  63.         $Directory.EnumerateDirectories() | ForEach-Object {
  64.             Safe-EnumerateFiles -Directory $_
  65.         }
  66.     } catch [System.UnauthorizedAccessException] {
  67.         Write-Host "Access denied: $($_.Exception.Message)"
  68.     }
  69.  
  70.     # Handle files
  71.     try {
  72.         $Directory.EnumerateFiles() | ForEach-Object {
  73.             $file = $_
  74.             $sizeInKb = "{0:N2}" -f ($file.Length / 1KB)
  75.             try {
  76.                 $acl = Get-Acl -LiteralPath $file.FullName
  77.                 $permissions = Get-PermissionsSummary -Acl $acl
  78.             } catch {
  79.                 $permissions = "Failed to retrieve permissions"
  80.             }
  81.             $line = "$($file.FullName),$sizeInKb,$($file.LastAccessTime),$($file.LastWriteTime),$($file.CreationTime),`"$permissions`""
  82.            
  83.             Add-Line -line $line -outputCsv $global:outputCsv
  84.         }
  85.     } catch [System.UnauthorizedAccessException] {
  86.         Write-Host "Access denied: $($_.Exception.Message)"
  87.     }
  88. }
  89.  
  90. # Create the first output CSV file
  91. $outputCsv = New-OutputCsv -basePath $baseOutputCsv -counter ([ref]$fileCounter)
  92.  
  93. # Start the enumeration
  94. Safe-EnumerateFiles -Directory (New-Object System.IO.DirectoryInfo $rootPath)
  95.  
  96. Write-Host "File details export completed. Files start with $baseOutputCsv`_1.csv"
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement