Das Problem liegt daran dass das Cmdlet Get-ChildItem mit der Option -Recurse keine versteckten oder Systemdateien mit einbezieht (das gilt genauso für Verzeichnisse). Du kannst die Rekursion stattdessen selbst in einer Funktion implementieren.
function Get-AllFolders {
param (
[string]$RootPath
)
$allFolders = New-Object System.Collections.Generic.List[string]
function RecurseFolders {
param (
[string]$path,
[ref]$folderList
)
Write-Host "Untersuche: $path" -ForegroundColor Yellow
try {
$items = Get-ChildItem -LiteralPath $path -Force -ErrorAction Stop
} catch {
Write-Host " Fehler beim Zugriff auf '$path': $_" -ForegroundColor Red
return
}
foreach ($item in $items) {
if ($item.PSIsContainer) {
Write-Host " ➜ Gefundener Ordner: $($item.FullName)" -ForegroundColor Gray
$folderList.Value.Add($item.FullName)
RecurseFolders -path $item.FullName -folderList $folderList
}
}
}
$allFolders.Add($RootPath)
RecurseFolders -path $RootPath -folderList ([ref]$allFolders)
return $allFolders
}
Die Funktion gibt alle gefundenen Ordner als Array zurück.
$path = "D:\Shares\Testshare"
$user = "DOMAIN\User"
$folders = Get-AllFolders -RootPath $path
foreach ($folder in $folders) {
$acl = Get-Acl $folder.FullName
$matches = $acl.Access | Where-Object {
$_.IdentityReference -eq $user -and $_.IsInherited -eq $false
}
if ($matches) {
Write-Host "Bearbeite Ordner: $($folder.FullName)" -ForegroundColor Cyan
foreach ($match in $matches) {
Write-Host " Entferne: $($match.FileSystemRights) ($($match.AccessControlType))"
$acl.RemoveAccessRule($match) | Out-Null
}
try {
Set-Acl -Path $folder.FullName -AclObject $acl
Write-Host " ? Rechte erfolgreich entfernt" -ForegroundColor Green
} catch {
Write-Host " ? Fehler beim Setzen der ACL: $_" -ForegroundColor Red
}
}
}