pixel0815
Goto Top

Update NTFS ACL eines Verzeichnisses

Hi zusammen,

ich hatte ja im vorherigen Post ein Skript gefunden und ich spiele damit gerade herum
Momentan habe ich ein Problem, aber aktuell seh ich den Wald vor lauter Bäumen nicht.

Es sollen die Berechtigungen auf der Ebene

\\File\TestOrdner aktualisiert werden ( also bei einem Match den neuen Account nehmen und z.B. Domänen-Benutzer ersetzen, bei einem neuen eintrag ohne Match den Benutzer entsprechend Berechtigungen mit Angabe z.B. FULLControl oder ohne Angabe - Modify ).

Der Inhalt vom TestOrdner hat die Berechtigungen so wie in der Tabelle aber nicht der Ordner selbst.
Hilfe, Freitag. Irgendwie ist da total der Wurm drin. Findet das jemand?

Matching Tabelle

OldAccount,NewAccount,AccessRights
TEST\Domänen-Benutzer,TEST1\123456
,TEST1\1234t,FullControl
,TEST1\Admins,

Param(
    # Parameter for the Search Base. This is the root folder path.
    [Parameter(Mandatory=$true)]
    [string]$SearchBase,

    # Parameter for the path to the mapping CSV file.
    [Parameter(Mandatory=$true)]
    [string]$MappingCsvPath,

    # Parameter for the log file path.
    [Parameter(Mandatory=$true)]
    [string]$LogFilePath,

    # Optional parameter to keep old permissions.
    [Parameter(Mandatory=$false)]
    [switch]$KeepOldPermissions
)

# Function to log messages to the log file.
function Write-Log {
    param (
        [string]$Message,
        [string]$LogType = "INFO"  
    )
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"  
    $logMessage = "[$timestamp][$LogType] $Message"  
    Add-Content -Path $LogFilePath -Value $logMessage
}

# Import the NTFS Security PowerShell Module
Try {
    Import-Module NTFSSecurity
    Write-Log "NTFS Security PowerShell Module imported successfully."  
} Catch {
    Write-Log "The NTFS Security PowerShell Module could not be imported. Please check that it is installed." -LogType "ERROR"  
    Exit
}

# Load the mapping CSV into a hashtable for quick lookup.
$mappingTable = @{}
Try {
    Import-Csv -Path $MappingCsvPath -Encoding Unicode | ForEach-Object {
        $oldAccount = $_.OldAccount
        $newAccount = $_.NewAccount
        $accessRights = if ($_.AccessRights) { $_.AccessRights } else { "Modify" }  

        # Store in the mapping table
        $mappingTable[$newAccount] = @{
            OldAccount = $oldAccount
            AccessRights = $accessRights
        }
    }
    Write-Log "Mapping CSV loaded successfully."  
} Catch {
    Write-Log "Failed to load the mapping CSV from $MappingCsvPath." -LogType "ERROR"  
    Exit
}

function Update-ACL {
    param (
        [string]$Path
    )

    # Get current ACLs
    $acl = Get-NTFSAccess -Path $Path -ExcludeInherited 

    # Track existing accounts to check if new permissions need to be added
    $existingAccounts = @($acl | ForEach-Object { $_.Account.AccountName })

    ForEach ($newAccount in $mappingTable.Keys) {
        $oldAccount = $mappingTable[$newAccount].OldAccount
        $accessRights = $mappingTable[$newAccount].AccessRights

        if ($oldAccount -and $existingAccounts -contains $oldAccount) {
            # Update existing permissions if there's a matching old account  
            Write-Log "Updating $oldAccount to $newAccount on $Path with $accessRights rights."  

            Try {
                # Optionally remove the old ACE
                If (-not $KeepOldPermissions.IsPresent) {
                    Remove-NTFSAccess -Path $Path -Account $oldAccount -AccessRights $accessRights
                    Write-Log "Removed old ACE for $oldAccount on $Path"  
                }

                # Add new ACE
                Add-NTFSAccess -Path $Path -Account $newAccount -AccessRights $accessRights
                Write-Log "Added new ACE for $newAccount on $Path"  
            } Catch {
                Write-Log "Failed to update ACL for $Path" -LogType "ERROR"  
            }

        } elseif (-not $existingAccounts -contains $newAccount) {
            # Add new permissions if the account does not exist in current ACLs
            Write-Log "Adding new ACE for $newAccount on $Path with $accessRights rights."  
            Try {
                Add-NTFSAccess -Path $Path -Account $newAccount -AccessRights $accessRights
                Write-Log "Added new ACE for $newAccount on $Path"  
            } Catch {
                Write-Log "Failed to add new ACL for $Path" -LogType "ERROR"  
            }
        } else {
            Write-Log "No action needed for $newAccount on $Path"  
        }
    }
}

#region Top-Level Folder

# Update ACLs for the top-level folder
$topFolder = Get-Item -Path $SearchBase
Update-ACL -Path $topFolder.FullName

#endregion

#region Child Folders and Files

# Get all the child folders and files.
$children = Get-ChildItem -Path $SearchBase -Recurse

ForEach ($child in $children) {
    Update-ACL -Path $child.FullName
}

#endregion

# Log completion of the script.
Write-Log "NTFS permissions update completed for $SearchBase."  

Content-ID: 667772

Url: https://administrator.de/contentid/667772

Ausgedruckt am: 28.09.2024 um 18:09 Uhr

pixel0815
pixel0815 30.08.2024 um 13:45:54 Uhr
Goto Top
So funktioniert das zumindest das ein Treffer auf der Ebene Hauptverzeichnis aktualisiert wird. Aber dennoch neue Berechtigungen nicht hinzugefügt werden auf der ebene.

Alle Ordner und Dateien die unterhalb des Hauptverzeichnisses liegen, bekommen auch die neuen Berechtigungen.
Nur der Hauptordner nicht. face-sad((( Och man


Param(
    # Parameter für den Suchpfad (Search Base). Dies ist der Stammordnerpfad.
    [Parameter(Mandatory=$true)]
    [string]$SearchBase,

    # Parameter für den Pfad zur Mapping-CSV-Datei.
    [Parameter(Mandatory=$true)]
    [string]$MappingCsvPath,

    # Parameter für den Pfad zur Logdatei.
    [Parameter(Mandatory=$true)]
    [string]$LogFilePath,

    # Optionaler Parameter, um alte Berechtigungen beizubehalten.
    [Parameter(Mandatory=$false)]
    [switch]$KeepOldPermissions
)

# Funktion, um Nachrichten in die Logdatei zu schreiben.
function Write-Log {
    param (
        [string]$Message,
        [string]$LogType = "INFO"  
    )
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"  
    $logMessage = "[$timestamp][$LogType] $Message"  
    Add-Content -Path $LogFilePath -Value $logMessage
}

# Importieren des NTFS Security PowerShell-Moduls
Try {
    Import-Module NTFSSecurity
    Write-Log "NTFS Security PowerShell Module erfolgreich importiert."  
} Catch {
    Write-Log "Das NTFS Security PowerShell Module konnte nicht importiert werden. Bitte überprüfen Sie, ob es installiert ist." -LogType "ERROR"  
    Exit
}

# Laden der Mapping-CSV in eine Hashtable für einen schnellen Zugriff.
$mappingTable = @{}
Try {
    Import-Csv -Path $MappingCsvPath -Encoding Unicode | ForEach-Object {
        $oldAccount = $_.OldAccount
        $newAccount = $_.NewAccount
        $accessRights = if ($_.AccessRights) { $_.AccessRights } else { "Modify" }  

        # In der Mapping-Tabelle speichern
        $mappingTable[$newAccount] = @{
            OldAccount = $oldAccount
            AccessRights = $accessRights
        }
    }
    Write-Log "Mapping CSV erfolgreich geladen."  
} Catch {
    Write-Log "Fehler beim Laden der Mapping-CSV von $MappingCsvPath." -LogType "ERROR"  
    Exit
}

function Update-ACL {
    param (
        [string]$Path
    )

    # Aktuelle ACLs abrufen
    $acl = Get-NTFSAccess -Path $Path -ExcludeInherited 

    # Bestehende Konten erfassen, um zu überprüfen, ob neue Berechtigungen hinzugefügt werden müssen
    $existingAccounts = @($acl | ForEach-Object { $_.Account })

    ForEach ($newAccount in $mappingTable.Keys) {
        $oldAccount = $mappingTable[$newAccount].OldAccount
        $accessRights = $mappingTable[$newAccount].AccessRights

        if ($oldAccount -and $existingAccounts -contains $oldAccount) {
            # Aktualisierung bestehender Berechtigungen, wenn ein übereinstimmendes altes Konto gefunden wird
            Write-Log "Aktualisiere $oldAccount zu $newAccount auf $Path mit $accessRights-Rechten."  

            Try {
                # Optionale Entfernung der alten ACE
                If (-not $KeepOldPermissions.IsPresent) {
                    Remove-NTFSAccess -Path $Path -Account $oldAccount -AccessRights $accessRights
                    Write-Log "Alte ACE für $oldAccount auf $Path entfernt."  
                }

                # Neue ACE hinzufügen
                Add-NTFSAccess -Path $Path -Account $newAccount -AccessRights $accessRights
                Write-Log "Neue ACE für $newAccount auf $Path hinzugefügt."  
            } Catch {
                Write-Log "Fehler beim Aktualisieren der ACL für $Path" -LogType "ERROR"  
            }

        } elseif (-not $existingAccounts -contains $newAccount) {
            # Neue Berechtigungen hinzufügen, wenn das Konto nicht in den aktuellen ACLs existiert
    
            Write-Log "Neue ACE für $newAccount auf $Path mit $accessRights-Rechten hinzufügen."  
            Try {
                Add-NTFSAccess -Path $Path -Account $newAccount -AccessRights $accessRights
                Write-Log "Neue ACE für $newAccount auf $Path hinzugefügt."  
            } Catch {
                Write-Log "Fehler beim Hinzufügen neuer ACL für $Path" -LogType "ERROR"  
            }
        } else {
            Write-Log "Keine Aktion erforderlich für $newAccount auf $Path."  
        }
    }
}

#region Aktualisierung von Berechtigungen für das Suchverzeichnis

# Aktualisiere ACLs für das Hauptverzeichnis (SearchBase)
Update-ACL -Path $SearchBase

#endregion

#region Aktualisierung von Berechtigungen für untergeordnete Dateien und Verzeichnisse

# Hole alle untergeordneten Verzeichnisse und Dateien.
$children = Get-ChildItem -Path $SearchBase -Recurse

ForEach ($child in $children) {
    Update-ACL -Path $child.FullName
}

#endregion

# Abschlussmeldung ins Log schreiben.
Write-Log "NTFS-Berechtigungsupdate für $SearchBase abgeschlossen."