NTFS Permissions Auditing with PowerShell

I read a brilliant post a few months ago, dealing with a similar situation by essentially running a script remotely that uses the Get-ACL cmdlet to list acl's for a path recursively, and piping the output trough the Export-CSV cmdlet for a nice overview:

http://jfrmilner.wordpress.com/2011/05/01/audit-ntfs-permissions-powershell-script/


In this case, I think the Sysinternals AccessChk and AccessEnum tools might be a better fit for what you are looking for. They can be found under File and Disk Utilities here.


Similar to the link Judaslscariot1651 provided. I went more toward taking the snapshot of what I know is good and then comparing it whenever I needed by running a script. I compared the file permissions by outputting to XML what it currently found the permissions to be and then compare that to my baseline XML file using Compare-Object. May not be exact, but just the way I went about doing it...

Note: this was a work in progress at the time and is geared toward particular paths I needed to watch, that I pulled from reading in registry key values. The main gist of what you probably needs starts near "Collecting information on...". I just wanted to provide all of the code to show how/what I was doing.



#Get date/time for file name
$d = Get-Date -format "yyyyMMdd"
# Pull instance names found on server 
$inst = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances

$warningpreference = "Continue"

foreach($i in $inst) 
{    $p = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').$i
    $bin = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$p\Setup").SQLBinRoot
    $ver = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$p\MSSQLServer\CurrentVersion").CurrentVersion
    switch -wildcard ($ver) 
    {
        "9*" {$client = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\90').VerSpecificRootDir; break }
        "10*" {$client = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\100').VerSpecificRootDir; break }
    }

    $currentFile = $i + "_" + $d + ".xml"
    "Collecting information on $bin and $client for $i"
        $bin,$client | foreach { Get-ChildItem $_ -Recurse } |
                    Select FullName, CreationTimeUTC, LastWriteTimeUtc, PSIsContainer, Length,
                    @{Name='Owner';Expression={ (Get-Acl $_.PSPath).Owner }},
                    @{Name='ACLRights';Expression={ (Get-Acl $_.PSPath).Access | Select FileSystemRights }},
                    @{Name='ACLUser';Expression={ (Get-Acl $_.PSPath).Access | Select IdentityReference }},
                    @{Name='ACLAccessType';Expression={ (Get-Acl $_.PSPath).Access | Select AccessControlType}} |
                        Export-Clixml $currentFile
    "Current file is: $currentFile"

    $basefile = "baseline_$i.xml"
    if (Test-Path $basefile)
    {
        $base = Import-Clixml $basefile
        $current = Import-Clixml $currentFile
        #now compare
        $results = "Results_" + $i + "_" + $d + ".txt"
        Compare-Object $base $current -Property CreationTimeUTC, LastWriteTimeUtc, Length, FullName, Owner, AclRights, AclUser, AclAccessType |
            Out-File $results

        #determine if the results file shows any changes, if so wave a flag
        if ((Get-Content $results).Length -eq $null)
        {
            "ZERO Changes found"
        }
        else
        {
            "changes found"
        }
    }
    else
    {
        Write-Warning -Message "WARNING: No base file found to compare."
    }
}