Welcome to Powergui.org - an open source community for Windows Powershell

PowerGUI.org PowerGUI.org and blogs

Forums » Request a Script

Thread: Script to compare permissions on directories

This question is answered.


Permlink Replies: 2 - Pages: 1 - Last Post: Nov 30, 2009 4:36 PM by: RTeal
RTeal

Posts: 27
Registered: 11/20/09
Script to compare permissions on directories
Posted: Nov 23, 2009 3:04 PM
 
  Click to reply to this thread Reply

Hello,

I've been trying to find a script that can show me how to traverse the directory tree comparing the permissions on a subdirectory to the parent directory and if different, write the directory name to a file.

I'm new to powershell and haven't gotten much beyond comparing two text files.
I'd appreciate any help and will make it a sincere learning experience.
Thanks,
Robert




antize


Posts: 122
Registered: 2/21/09
Re: Script to compare permissions on directories
Posted: Nov 25, 2009 2:47 PM   in response to: RTeal
Answered
  Click to reply to this thread Reply

This script will do what your asking I think. By default it dumps folders that have different ACLs to C:\AclCompareDump.txt

[System.IO.FileInfo] $outputFile = "C:\AclCompareDump.txt"
[System.IO.DirectoryInfo] $searchDir = Read-Host -Prompt "Enter folder path to search"

if ($outputFile.Exists)
{
    $outputFile.Delete()
}

if ($searchDir.Exists)
{
    $refacl = Get-Acl $searchDir.Fullname
    dir -Path $searchDir.Fullname -Recurse -Force | ? {$_.PsIsContainer} | % {
        $currentFolder = $_
        Write-Progress -Activity "Searching for folder ACLs..." -CurrentOperation $currentFolder.FullName -Status "Current Folder:"
        $currentAcl = Get-Acl $currentFolder.FullName
        $comparison = compare-object $refacl $currentAcl -Property Access
        if ($comparison -ne $null)
        {
            $currentFolder.Fullname | Out-File $outputFile.FullName -Append
            [int] $hits += 1
        }
        $comparison = $null
    }
   
    if ($hits)
    {
        & notepad.exe $outputFile.Fullname
    }
    else
    {
        Write-Warning "All sub folder ACLs matched the parent folder ACL."
    }
}
else
{
    Write-Error ($searchDir.Fullname + " does not exist.")
}




RTeal

Posts: 27
Registered: 11/20/09
Re: Script to compare permissions on directories
Posted: Nov 30, 2009 4:36 PM   in response to: RTeal
 
  Click to reply to this thread Reply

Antize,

Thanks. This works like a charm and gives me some good learning points going forward. I appreciate your help.

Robert


Legend
MVP: 2501 + pts
Guru: 2001 - 2500 pts
Expert: 751 - 2000 pts
Enthusiast: 31 - 750 pts
Novice: 0 - 30 pts
Moderators
Helpful answer (5 pts)
Answered (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums