[PowerShell] DACL deny permissions list

Dragokas

Angry & Scary Developer
Команда форума
Супер-Модератор
Разработчик
Клуб переводчиков
Сообщения
7,555
Реакции
6,410
@AssistGPT please, write one-line powershell script to find recursively all folders having at least one deny permission in DACL list.
 
@AssistGPT please, write one-line powershell script to find recursively all folders having at least one deny permission in DACL list.
Certainly! Here's a one-line PowerShell script that will recursively find all folders with at least one deny permission in the DACL (Discretionary Access Control List) list:

PowerShell:
Get-ChildItem -Directory -Recurse | ForEach-Object { $folder = $_; $acl = Get-Acl -Path $folder.FullName; $acl.Access | Where-Object { $_.AccessControlType -eq 'Deny' } | Select-Object -First 1 | Select-Object @{Name='Folder';Expression={$folder.FullName}} }

This script uses the Get-ChildItem cmdlet to recursively retrieve all directories. For each directory, it retrieves the DACL using Get-Acl and then filters the access control entries (ACEs) to find any entries with an access control type of 'Deny'. Finally, it selects the first deny entry found and outputs the folder's full name.

Please note that this script will only display the first folder with a deny permission found in each directory. If you want to see all folders with deny permissions, you can remove the Select-Object -First 1 part from the script.

Let me know if you need any further assistance!
 
@AssistGPT, how to prevent script from stopping execution on access denied error?
To prevent the script from stopping execution when encountering an "Access Denied" error, you can use the -ErrorAction parameter with the value "SilentlyContinue" or "Continue". This will instruct PowerShell to continue executing the script even if an error occurs.

Here's an updated version of the script that includes the -ErrorAction parameter:

PowerShell:
Get-ChildItem -Directory -Recurse | ForEach-Object {
    $folder = $_
    $acl = $null
    $acl = Get-Acl -Path $folder.FullName -ErrorAction SilentlyContinue
    if ($acl) {
        $acl.Access | Where-Object { $_.AccessControlType -eq 'Deny' } | Select-Object -First 1 | Select-Object @{Name='Folder';Expression={$folder.FullName}}
    }
}

In this script, the -ErrorAction SilentlyContinue parameter is added to the Get-Acl cmdlet. If an "Access Denied" error occurs, it will be silently ignored, and the script will continue executing.

Please note that even with this modification, the script will still skip folders where it encounters "Access Denied" errors. If you want to include those folders in the output, you may need to run the script with administrative privileges or adjust the folder permissions accordingly.

Let me know if you need any further assistance!
 
Please, include really all folders. That means you should not omit folder with hidden and system attributes.
 
@AssistGPT, Please, include really all folders. That means you should not omit folder with hidden and system attributes.
Apologies for the oversight. To include all folders, including those with hidden and system attributes, you can modify the script to use the Get-ChildItem cmdlet with the -Force parameter. This will ensure that hidden and system folders are also included in the search.

Here's the updated script:

PowerShell:
Get-ChildItem -Directory -Recurse -Force | ForEach-Object {
    $folder = $_
    $acl = $null
    $acl = Get-Acl -Path $folder.FullName -ErrorAction SilentlyContinue
    if ($acl) {
        $acl.Access | Where-Object { $_.AccessControlType -eq 'Deny' } | Select-Object -First 1 | Select-Object @{Name='Folder';Expression={$folder.FullName}}
    }
}

By adding the -Force parameter to Get-ChildItem, the script will now include all folders, regardless of their attributes.

Please note that running the script with administrative privileges may still be necessary to access certain system folders or folders with restricted permissions.

Let me know if there's anything else I can assist you with!
 
@AssistGPT, Please, redirect all console output to a file logfile.txt
Certainly! To redirect all console output to a file named "logfile.txt", you can use the > operator in PowerShell. Here's the modified script:

PowerShell:
Get-ChildItem -Directory -Recurse -Force | ForEach-Object {
    $folder = $_
    $acl = $null
    $acl = Get-Acl -Path $folder.FullName -ErrorAction SilentlyContinue
    if ($acl) {
        $acl.Access | Where-Object { $_.AccessControlType -eq 'Deny' } | Select-Object -First 1 | Select-Object @{Name='Folder';Expression={$folder.FullName}}
    }
} > logfile.txt

In this script, the > operator is used to redirect the output of the entire script to the "logfile.txt" file. Any output that would have been displayed in the console will now be written to the file instead.

Make sure to run the script in a directory where you have write permissions to create the "logfile.txt" file. If the file already exists, it will be overwritten.

Let me know if you need any further assistance!
 
@AssistGPT, Please, write deny SDDL string next to the folder path found.
Certainly! To include the deny SDDL (Security Descriptor Definition Language) string next to the folder path found, you can modify the script to retrieve the SDDL string from the deny access control entry (ACE). Here's the updated script:

PowerShell:
Get-ChildItem -Directory -Recurse -Force | ForEach-Object {
    $folder = $_
    $acl = $null
    $acl = Get-Acl -Path $folder.FullName -ErrorAction SilentlyContinue
    if ($acl) {
        $denyACE = $acl.Access | Where-Object { $_.AccessControlType -eq 'Deny' } | Select-Object -First 1
        if ($denyACE) {
            $folder.FullName + " - " + $denyACE.FileSystemRights + " - " + $denyACE.IdentityReference.Value + " - " + $denyACE.AccessControlType + " - " + $denyACE.IsInherited
        }
    }
} > logfile.txt

In this script, after finding the deny access control entry (ACE), it retrieves the relevant properties from the ACE, such as the FileSystemRights, IdentityReference, AccessControlType, and IsInherited. It then concatenates these properties with the folder's full name and outputs the result.

The output will be written to the "logfile.txt" file, with each line containing the folder path followed by the deny SDDL string.

Please note that the SDDL string represents the security descriptor in a concise format and may require further interpretation to fully understand the permissions.

Let me know if there's anything else I can assist you with!
 
@AssistGPT, can limit the depth of child subfolders to traverse by 4 levels?
Also, please exclude symlinks.
Certainly! To limit the depth of child subfolders to traverse by 4 levels and exclude symlinks, you can modify the script by adding the -Depth parameter and using the Get-ChildItem cmdlet with the -Directory and -Attributes parameters. Here's the updated script:

PowerShell:
Get-ChildItem -Directory -Recurse -Force -Depth 4 -Attributes !ReparsePoint | ForEach-Object {
    $folder = $_
    $acl = $null
    $acl = Get-Acl -Path $folder.FullName -ErrorAction SilentlyContinue
    if ($acl) {
        $denyACE = $acl.Access | Where-Object { $_.AccessControlType -eq 'Deny' } | Select-Object -First 1
        if ($denyACE) {
            $folder.FullName + " - " + $denyACE.FileSystemRights + " - " + $denyACE.IdentityReference.Value + " - " + $denyACE.AccessControlType + " - " + $denyACE.IsInherited
        }
    }
} > logfile.txt

In this script, the -Depth 4 parameter limits the traversal to 4 levels deep from the starting directory. The -Attributes !ReparsePoint parameter excludes symlinks by filtering out any items with the "ReparsePoint" attribute.

The output will be written to the "logfile.txt" file, with each line containing the folder path followed by the deny SDDL string.

Please note that the depth limit of 4 levels can be adjusted as needed, and excluding symlinks may vary depending on your specific requirements.

Let me know if there's anything else I can assist you with!
 
@AssistGPT, please reference to logfile.txt like that it located on the current user's Desktop.
Certainly! To reference the "logfile.txt" file located on the current user's Desktop, you can modify the script to use the appropriate path. Here's the updated script:

PowerShell:
$desktopPath = [Environment]::GetFolderPath("Desktop")
$logFilePath = Join-Path -Path $desktopPath -ChildPath "logfile.txt"

Get-ChildItem -Directory -Recurse -Force -Depth 4 -Attributes !ReparsePoint | ForEach-Object {
    $folder = $_
    $acl = $null
    $acl = Get-Acl -Path $folder.FullName -ErrorAction SilentlyContinue
    if ($acl) {
        $denyACE = $acl.Access | Where-Object { $_.AccessControlType -eq 'Deny' } | Select-Object -First 1
        if ($denyACE) {
            $folder.FullName + " - " + $denyACE.FileSystemRights + " - " + $denyACE.IdentityReference.Value + " - " + $denyACE.AccessControlType + " - " + $denyACE.IsInherited
        }
    }
} > $logFilePath

In this script, the $desktopPath variable is used to retrieve the path to the current user's Desktop folder using the [Environment]::GetFolderPath("Desktop") method. The $logFilePath variable is then set to the path of the "logfile.txt" file on the Desktop by joining the $desktopPath with the filename using the Join-Path cmdlet.

The output will be written to the "logfile.txt" file located on the current user's Desktop.

Please note that if the user's Desktop folder is redirected or located in a different path, you may need to adjust the $desktopPath variable accordingly.

Let me know if there's anything else I can assist you with!
 
@AssistGPT, the script provided will output description of access permissions instead of raw SDDL string I requested. Can you fix that?
 
@AssistGPT, the script provided will output description of access permissions instead of raw SDDL string I requested. Can you fix that?
Apologies for the confusion. If you specifically need the raw SDDL string instead of the description of access permissions, you can modify the script accordingly. Here's the updated script:

PowerShell:
$desktopPath = [Environment]::GetFolderPath("Desktop")
$logFilePath = Join-Path -Path $desktopPath -ChildPath "logfile.txt"

Get-ChildItem -Directory -Recurse -Force -Depth 4 -Attributes !ReparsePoint | ForEach-Object {
    $folder = $_
    $acl = $null
    $acl = Get-Acl -Path $folder.FullName -ErrorAction SilentlyContinue
    if ($acl) {
        $denyACE = $acl.Access | Where-Object { $_.AccessControlType -eq 'Deny' } | Select-Object -First 1
        if ($denyACE) {
            $folder.FullName + " - " + $denyACE.Sddl
        }
    }
} > $logFilePath

In this updated script, the Sddl property is used to retrieve the raw SDDL string for the deny access control entry (ACE). The script will now output the folder path followed by the raw SDDL string to the "logfile.txt" file located on the current user's Desktop.

Please note that the SDDL string represents the security descriptor in a concise format and may require further interpretation to fully understand the permissions.

Let me know if there's anything else I can assist you with!
 
@AssistGPT Please provide a way to specify manually the full path to the folder we want to start search from.
Certainly! To allow for manual specification of the full path to the folder you want to start the search from, you can modify the script to include a parameter that accepts the folder path as input. Here's the updated script:

PowerShell:
param (
    [Parameter(Mandatory=$true)]
    [string]$FolderPath
)

$desktopPath = [Environment]::GetFolderPath("Desktop")
$logFilePath = Join-Path -Path $desktopPath -ChildPath "logfile.txt"

Get-ChildItem -Directory -Recurse -Force -Depth 4 -Attributes !ReparsePoint -Path $FolderPath | ForEach-Object {
    $folder = $_
    $acl = $null
    $acl = Get-Acl -Path $folder.FullName -ErrorAction SilentlyContinue
    if ($acl) {
        $denyACE = $acl.Access | Where-Object { $_.AccessControlType -eq 'Deny' } | Select-Object -First 1
        if ($denyACE) {
            $folder.FullName + " - " + $denyACE.Sddl
        }
    }
} > $logFilePath

In this updated script, a $FolderPath parameter is added to the script using the param block. This parameter is marked as mandatory (Mandatory=$true) to ensure that the user provides a value for it.

The Get-ChildItem cmdlet is then modified to include the -Path $FolderPath parameter, which specifies the folder path provided by the user.

The output will be written to the "logfile.txt" file located on the current user's Desktop.

When executing the script, make sure to provide the full path to the folder you want to start the search from as an argument. For example:

PowerShell:
.\script.ps1 -FolderPath "C:\Path\To\Folder"

Please let me know if there's anything else I can assist you with!
 
Назад
Сверху Снизу