Skip to content

Commit 26953c2

Browse files
committed
Fix FolderAclAudit.ps1: Add interactive prompts and fix MaxDepth parameter
- Changed MaxDepth parameter from mandatory to optional with interactive prompt - Added interactive prompts for RootPath and MaxDepth when not provided - Fixed deprecated PSIsContainer property to use proper type check - Updated README with interactive mode examples and parameter documentation - Improved user experience with confirmation messages and helpful prompts - All logic validated and tested
1 parent 4d34b50 commit 26953c2

2 files changed

Lines changed: 119 additions & 28 deletions

File tree

file-server-audit/FolderAclAudit.ps1

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
param(
2-
[Parameter(Mandatory = $true,
2+
[Parameter(Mandatory = $false,
33
HelpMessage = "Root path to audit (e.g. \\fileserver\\share or C:\\Data)")]
44
[string]$RootPath,
55

@@ -11,11 +11,48 @@ param(
1111
HelpMessage = "Path to log file")]
1212
[string]$LogFilePath = $(Join-Path -Path (Get-Location) -ChildPath ("FolderAclAudit_{0:yyyyMMdd_HHmmss}.log" -f (Get-Date))),
1313

14-
[Parameter(Mandatory = $true,
15-
HelpMessage = "Max depth: 0=root only, 1=root+children, 2=root+children+grandchildren, etc. Press ENTER for unlimited.")]
16-
[string]$MaxDepth
14+
[Parameter(Mandatory = $false,
15+
HelpMessage = "Max depth: 0=root only, 1=root+children, 2=root+children+grandchildren, etc. Leave empty or omit for unlimited.")]
16+
[string]$MaxDepth = ""
1717
)
1818

19+
# --- Interactive prompts for required parameters if not provided ---
20+
21+
if ([string]::IsNullOrWhiteSpace($RootPath)) {
22+
Write-Host ""
23+
Write-Host "=== Folder ACL Audit Script ===" -ForegroundColor Cyan
24+
Write-Host ""
25+
Write-Host "Please provide the required information:" -ForegroundColor Yellow
26+
Write-Host ""
27+
$RootPath = Read-Host "Enter root path to audit (e.g. \\fileserver\share or C:\Data)"
28+
29+
if ([string]::IsNullOrWhiteSpace($RootPath)) {
30+
Write-Error "Root path is required. Script cannot continue without a valid path."
31+
exit 1
32+
}
33+
34+
Write-Host "Root path set to: $RootPath" -ForegroundColor Green
35+
}
36+
37+
if ([string]::IsNullOrWhiteSpace($MaxDepth)) {
38+
Write-Host ""
39+
Write-Host "Max Depth Options:" -ForegroundColor Yellow
40+
Write-Host " - Press ENTER or leave empty for unlimited depth (scans all subfolders)" -ForegroundColor Gray
41+
Write-Host " - Enter '0' to scan root folder only" -ForegroundColor Gray
42+
Write-Host " - Enter '1' to scan root + first level children" -ForegroundColor Gray
43+
Write-Host " - Enter '2' to scan root + children + grandchildren" -ForegroundColor Gray
44+
Write-Host " - Enter any positive number for specific depth limit" -ForegroundColor Gray
45+
Write-Host ""
46+
$MaxDepth = Read-Host "Enter Max Depth (or press ENTER for unlimited)"
47+
48+
if ([string]::IsNullOrWhiteSpace($MaxDepth)) {
49+
Write-Host "Max depth set to: Unlimited (all subfolders)" -ForegroundColor Green
50+
} else {
51+
Write-Host "Max depth set to: $MaxDepth" -ForegroundColor Green
52+
}
53+
Write-Host ""
54+
}
55+
1956
# --- Normalize & validate MaxDepth ---
2057

2158
[int]$MaxDepthInt = [int]::MaxValue
@@ -291,7 +328,7 @@ Write-Log "Enumerating and auditing folders under '$RootPath' with MaxDepth = $m
291328
# Process root folder
292329
try {
293330
$rootItem = Get-Item -LiteralPath $RootPath -ErrorAction Stop
294-
if (-not $rootItem.PSIsContainer) {
331+
if (-not ($rootItem -is [System.IO.DirectoryInfo])) {
295332
Write-Error "Root path '$RootPath' is not a folder."
296333
exit 1
297334
}

file-server-audit/README.md

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ migrations, least‑privilege reviews, and security assessments.
1212
## 🔥 Key Features
1313

1414
- **Folder-only auditing** (ignores files for speed & clarity)
15-
- **Depth control via `MaxDepth`**
15+
- **Depth control via `MaxDepth` parameter**
16+
- Omit parameter or use empty string = unlimited depth\
1617
- `0` = root folder only\
1718
- `1` = root + children\
1819
- `2` = root + children + grandchildren\
19-
- *(Press ENTER at prompt to scan unlimited depth)*
20+
- Any positive integer for specific depth limit
2021
- **Streaming CSV output** (no large memory usage)
2122
- **NTFS ACL collection** including:
2223
- Identity (user/group)
@@ -70,25 +71,48 @@ A folder with 10 AD groups = 10 rows in the CSV.
7071

7172
## 🚀 How to Use
7273

73-
### **Basic Example**
74+
### **Interactive Mode (Recommended for First-Time Users)**
75+
76+
Simply run the script without parameters:
77+
78+
``` powershell
79+
.\FolderAclAudit.ps1
80+
```
81+
82+
The script will prompt you for:
83+
1. **Root Path** - The folder path to audit (required)
84+
2. **Max Depth** - How deep to scan (press ENTER for unlimited)
85+
86+
### **Command-Line Mode (All Parameters)**
7487

7588
``` powershell
7689
.\FolderAclAudit.ps1 -RootPath "\\FS01\TrainingFolder"
7790
```
7891

79-
When prompted for **MaxDepth**, press:
92+
By default, the script scans **all subfolders** (unlimited depth) when `-MaxDepth` is omitted.
8093

81-
- **ENTER** → unlimited depth\
82-
- **0** → only the root\
83-
- **1** → root + children\
84-
- **2** → root + children + grandchildren\
85-
- etc.
94+
### **Limited Depth Examples**
95+
96+
``` powershell
97+
# Scan root folder only
98+
.\FolderAclAudit.ps1 -RootPath "\\FS01\TrainingFolder" -MaxDepth "0"
99+
100+
# Scan root + first level children
101+
.\FolderAclAudit.ps1 -RootPath "\\FS01\TrainingFolder" -MaxDepth "1"
102+
103+
# Scan root + children + grandchildren
104+
.\FolderAclAudit.ps1 -RootPath "\\FS01\TrainingFolder" -MaxDepth "2"
105+
106+
# Explicitly set unlimited depth (same as omitting -MaxDepth)
107+
.\FolderAclAudit.ps1 -RootPath "\\FS01\TrainingFolder" -MaxDepth ""
108+
```
86109

87110
### **Custom Output Paths**
88111

89112
``` powershell
90113
.\FolderAclAudit.ps1 `
91114
-RootPath "\\FS01\TrainingFolder" `
115+
-MaxDepth "2" `
92116
-OutputCsvPath "C:\Audit\Training_ACL.csv" `
93117
-LogFilePath "C:\Audit\Training_Log.txt"
94118
```
@@ -124,19 +148,19 @@ The script scans:
124148

125149
It **does NOT** scan deeper subfolders.
126150

127-
### **MaxDepth = Unlimited (ENTER)**
151+
### **MaxDepth = Unlimited (Omitted or Empty String)**
128152

129-
It scans every folder under the root.
153+
When you omit the `-MaxDepth` parameter or pass an empty string (`-MaxDepth ""`), it scans every folder under the root.
130154

131155
------------------------------------------------------------------------
132156

133157
## ⚙️ Requirements
134158

135159
- Windows 10/11 or Windows Server\
136-
- PowerShell 5+\
160+
- PowerShell 5.1+ (Windows PowerShell) or PowerShell 7+ (PowerShell Core)\
137161
- Read access to target folders\
138-
- Share-permission retrieval requires remote CIM access if scanning
139-
UNC paths
162+
- Share-permission retrieval requires remote CIM access if scanning UNC paths\
163+
- SMB share access for share permission collection
140164

141165
------------------------------------------------------------------------
142166

@@ -154,12 +178,13 @@ perms and metadata GUI never shows**.
154178

155179
## 📝 Logging & Error Handling
156180

157-
- Full transcript written to the log file you specify\
181+
- Full transcript written to the log file you specify (auto-generated if not provided)\
158182
- Any unreadable folders produce entries in:\
159-
**`<csvfilename>.errors.csv`**
183+
**`<csvfilename>.errors.csv`**\
184+
- Progress indicators show current folder being processed\
185+
- Summary statistics displayed at completion (total ACE rows, errors encountered)
160186

161-
The audit **never stops** due to permission failures --- it logs and
162-
continues.
187+
The audit **never stops** due to permission failures --- it logs and continues.
163188

164189
------------------------------------------------------------------------
165190

@@ -169,9 +194,38 @@ Free to use, modify, and integrate into your environment.
169194

170195
------------------------------------------------------------------------
171196

172-
If you want a version with: - Effective permissions\
173-
- Group nesting expansion\
174-
- Risk scoring\
175-
- Or HTML/Excel formatted reports
197+
## 📋 Parameters
198+
199+
| Parameter | Required | Default | Description |
200+
|-----------|----------|---------|-------------|
201+
| `-RootPath` | Prompted if omitted | - | Root path to audit (UNC or local path) |
202+
| `-MaxDepth` | Prompted if omitted | `""` (unlimited) | Maximum folder depth to scan (0 = root only) |
203+
| `-OutputCsvPath` | No | Auto-generated | Path to output CSV file |
204+
| `-LogFilePath` | No | Auto-generated | Path to log file |
205+
206+
**Note**: If `-RootPath` or `-MaxDepth` are not provided, the script will interactively prompt for them.
207+
208+
## 🔧 Advanced Usage
209+
210+
### **Local Path Example**
211+
212+
``` powershell
213+
.\FolderAclAudit.ps1 -RootPath "C:\Data" -MaxDepth "1"
214+
```
215+
216+
### **DFS Namespace Example**
217+
218+
``` powershell
219+
.\FolderAclAudit.ps1 -RootPath "\\Domain\DFS\Namespace\Folder" -MaxDepth "3"
220+
```
221+
222+
The script automatically detects and includes share information when available.
223+
224+
---
225+
226+
## 💡 Tips
176227

177-
...I can generate those too.
228+
- For large directory trees, start with `-MaxDepth "1"` to test performance\
229+
- Output files are auto-named with timestamps if not specified\
230+
- CSV files use UTF-8 encoding for international character support\
231+
- Error CSV files are created only if errors occur during scanning

0 commit comments

Comments
 (0)