Popraw poniższy skrypt wyświetlający użytkowników mających dostęp do folderu i…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Popraw poniższy skrypt wyświetlający użytkowników mających dostęp do folderu i podfolderów (1 poziom niżej) tak aby jako jego parametr podawać ścieżkę_i_nazwę_folderu oraz aby wyświetlał również użytkowników z grup które są na tej liście z wyłączeniem użytkowników z grup systemowych Active Directory (takich jak np. DOMENA\Administrators czy NT AUTHORITY\SYSTEMS), oraz jeśli grupy te zawierają inne grupy to również użytkowników w tych dalszych grupach.
Import-Module ActiveDirectory
$path = "\das\fs\grupowe\folder-0015" # Zmień na swoją ścieżkę
function Get-FullName { param ($identityReference)
# Sprawdź, czy identityReference jest w formacie DOMENA\nazwa_uzytkownika
if ($identityReference -match "\\") {
$parts = $identityReference.ToString().Split("\")
$domain = $parts[0]
$samAccountName = $parts[1]
# Spróbuj pobrać użytkownika z AD
try {
$adUser = Get-ADUser -Identity $samAccountName -Properties DisplayName, GivenName, Surname
# Jeśli DisplayName jest dostępne, użyj go, w przeciwnym razie połącz GivenName i Surname
if ($adUser.DisplayName) {
return $adUser.DisplayName
} elseif ($adUser.GivenName -and $adUser.Surname) {
return "$($adUser.GivenName) $($adUser.Surname)"
} else {
return $identityReference # Użyj oryginalnej wartości, jeśli nie można pobrać imienia i nazwiska
}
} catch {
# Jeśli to nie jest użytkownik AD lub wystąpił błąd, sprawdź czy to grupa
try {
$adGroup = Get-ADGroup -Identity $samAccountName -Properties Name
return "$($adGroup.Name) (grupa)"
} catch {
# Jeśli to nie jest ani użytkownik, ani grupa, zwróć oryginalną wartość
return $identityReference
}
}
} else {
# Jeśli nie jest w formacie DOMENA\nazwa_uzytkownika, zwróć oryginalną wartość
return $identityReference
}
}
function Show-ACL { param (folderPath" -ForegroundColor Green folderPath acl.Access | ForEach-Object { identity = fullName = Get-FullName -identityReference rights = type = _.AccessControlType # Uproszczenie: RW jeśli jest Write, RO jeśli tylko Read, inaczej pełne simpleRights = if (rights -match "Read") { "RO" } else { rights.ToString() } Write-Host "Użytkownik/Grupa: fullName | Uprawnienia: rights) | Typ: $type" } Write-Host "" # Linia odstępu }
Show-ACL -folderPath $path
path -Directory -Depth 0 # Depth 0 to tylko bezpośrednie dzieci
foreach (subfolders) { Show-ACL -folderPath $sub.FullName }
Poniżej znajduje się kompletny, poprawiony skrypt spełniający wszystkie Twoje wymagania:
NT AUTHORITY\SYSTEM, BUILTIN\Administrators, itp.),Get-FolderAccess.ps1[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$Path
)
Import-Module ActiveDirectory -ErrorAction Stop
# --- Funkcja: czy to konto systemowe? ---
function Is-SystemAccount {
param([string]$Identity)
$systemPatterns = @(
'NT AUTHORITY\\',
'BUILTIN\\',
'CREATOR OWNER',
'Everyone',
'Authenticated Users',
'Domain Computers',
'Domain Users',
'S-1-5-'
)
foreach ($pattern in $systemPatterns) {
if ($Identity -match $pattern) { return $true }
}
return $false
}
# --- Funkcja: pobierz pełne imię i nazwisko ---
function Get-FullName {
param([string]$SamAccountName)
try {
$user = Get-ADUser -Identity $SamAccountName -Properties DisplayName, GivenName, Surname
if ($user.DisplayName) { return $user.DisplayName }
return "$($user.GivenName) $($user.Surname)".Trim()
} catch {
return $SamAccountName
}
}
# --- Funkcja: pobierz użytkowników z grupy (rekurencyjnie) ---
function Get-UsersFromGroup {
param([string]$GroupSam)
try {
$members = Get-ADGroupMember -Identity $GroupSam -Recursive | Where-Object { $_.objectClass -eq 'user' }
return $members.SamAccountName | Sort-Object -Unique
} catch {
return @()
}
}
# --- Funkcja: przetwórz ACL folderu ---
function Get-FolderAccess {
param([string]$FolderPath)
Write-Host "`n📁 $FolderPath" -ForegroundColor Cyan
$acl = Get-Acl -Path $FolderPath
$uniqueUsers = @{}
foreach ($access in $acl.Access) {
$identity = $access.IdentityReference.ToString()
# Pomijamy systemowe
if (Is-SystemAccount -Identity $identity) { continue }
# Rozbij na domenę i nazwę
if ($identity -match '^(.*?)\\(.+)$') {
$domain = $matches[1]
$name = $matches[2]
} else {
$name = $identity
}
# Jeśli to grupa → pobierz użytkowników
try {
$isGroup = Get-ADObject -Identity $name -Properties objectClass | Where-Object { $_.objectClass -eq 'group' }
if ($isGroup) {
$users = Get-UsersFromGroup -GroupSam $name
foreach ($u in $users) {
$uniqueUsers[$u] = $true
}
} else {
$uniqueUsers[$name] = $true
}
} catch {
# Jeśli nie można rozpoznać w AD, pomiń
continue
}
}
# Wyświetl użytkowników
foreach ($sam in $uniqueUsers.Keys) {
$fullName = Get-FullName -SamAccountName $sam
Write-Host " 👤 $fullName"
}
}
# --- Główna logika ---
if (-not (Test-Path -Path $Path)) {
Write-Error "Ścieżka nie istnieje: $Path"
exit 1
}
# Folder główny
Get-FolderAccess -FolderPath $Path
# Podfoldery (1 poziom)
Get-ChildItem -Path $Path -Directory -Depth 0 | ForEach-Object {
Get-FolderAccess -FolderPath $_.FullName
}
.\Get-FolderAccess.ps1 -Path "\\das\fs\grupowe\folder-0015"
📁 \\das\fs\grupowe\folder-0015
👤 Jan Kowalski
👤 Anna Nowak
📁 \\das\fs\grupowe\folder-0015\Podfolder1
👤 Piotr Wiśniewski
Jeśli chcesz zapisać wynik do pliku TXT, uruchom:
.\Get-FolderAccess.ps1 -Path "\\das\fs\grupowe\folder-0015" > "C:\Temp\access_report.txt"
Czy chcesz wersję z eksportem do CSV lub z GUI (wybór folderu)?