Audit Folders/Shares and Export to CSV file

# NAME: AuditPerms2CSV.ps1
#
# AUTHOR: OldDog
# DATE : 11/12/2014
#
# COMMENT: Audit Folders/Shares and Export to CSV file
# This saves the results in a directory called C:\powershell\results
# if you don't have that directory it will create one.
# It also deletes any existing copy of Result.csv so if you want to save them rename them.
# ==============================================================================================
$path = $args[0]

if (! $args ) {
$path = Read-Host “Please enter the full path name you wish to check, like this: \\FileServer\share\user”

}

If (Test-path “C:\Powershell\Results”) {
If (Test-path “C:\Powershell\Results\Result.csv”) {
del “C:\Powershell\Results\Result.csv” }
}
Else {
New-Item -Path “C:\Powershell\Results” -ItemType directory
}

Add-Content “C:\Powershell\Results\Result.csv” “Folder Path:, Users/Groups:,Permissions:,Permissions Inherited:”

####Change the path to the folder or share you want NTFS perms on####
$dirToAudit = Get-ChildItem -Path $path -recurse | Where {$_.psIsContainer -eq $true}

foreach ($dir in $dirToAudit)
{
$colACL = Get-Acl -Path $dir.FullName

foreach ($acl in $colACL)
{
If ($acl.Access | ? inheritanceflags -eq ‘none’){
$In = $false } Else {$In = $True }

foreach ($accessRight in $acl.Access)
{
$DirName = $dir.FullName
$ar = $($AccessRight.FileSystemRights)
$id = $($AccessRight.IdentityReference)
Add-Content “C:\Powershell\Results\Result.csv” “$dirName ,$id,””$ar””,$In” #Note the “” to escape the ‘,’ in the permissions
}
$In = $null
}

}

How do I Find all the Non Inherited perms on a list of dirs.


COMMENT: Finds all the Non Inherited perms on a list of dirs.
Change where {$_.IsInherited -ne $true} to where {$_.IsInherited -eq $true} for Inherited perms.
Or you can use ? instead of where ie. ? {$_.IsInherited -eq $true}
==============================================================================================
#>
###### Start Posh Script ########

$result = @()
$paths = gc C:\scripts\ps1\dirs.txt
foreach ($path in $paths) {
$all = dir $path -rec
$dirs = $all | ? {$_.psiscontainer -eq "true"}
$result += $dirs | Get-Acl | %{$_.Access | Add-Member -MemberType NoteProperty Path -Value ($_.Path -replace ".+::") -PassThru} |
select Path,FileSystemRights,AccessControlType,IdentityReference,IsInherited,InheritanceFlags,PropagationFlags |
where {$_.IsInherited -ne $true}
}

$result | Export-Csv mypaths.csv –NoTypeInformation