You have to CD to the folder where you saved the script or add the full path to it.
I have updated the code to generate icacls commands to grant access for whatever account you wish. You would need to redirect the output to a file and then execute the commands after you verify that they will make the correct changes. I also added a depth switch.
.\FindUnInheritedPerms.ps1 -target c:\temp -grant "BUILTIN\Administrators:(OI)(CI)(F)" -depth 1
<#
.SYNOPSIS
This is a simple Powershell script to analyze a given folder structure and look to see what files/folder have uninherited ACL's
.DESCRIPTION
Find files/folder where admins/owners have been tweaking security permissions.
This script accepts these parameters.
-target The path to the folder to be analyzed.
-all If true, analyze files in addition to folders.
-depth How many subfolders to analyze
-grant Generate icacls /grant command for each folder which does not inherit any permissions.
.EXAMPLE
./FindUnInheritedPerms.ps1 -target c:\temp -all $true
./FindUnInheritedPerms.ps1 -target c:\temp
./FindUnInheritedPerms.ps1 -target c:\temp -depth 2 /grant "BUILTIN\Administrators:(OI)(CI)(F)"
.NOTES
.LINK
http://www.google.com
Author: MotoX80 on Microsoft Q&A Forums
#>
param (
[string]$target = '', # analyze this folder
[boolean]$all = $false, # include files
[string]$grant = '', # generate icacls commands
[string]$depth = '9999999' # folder depth to analyze
)
if ($target -eq '') {
"Please specify a target folder to analyze."
return
}
if ($grant -eq '') {
"Base permissions on $target"
Get-Acl -Path $target | select-object -ExpandProperty access | format-table -Property IdentityReference, AccessControlType, FileSystemRights, IsInherited
}
if ($all) {
$folders = Get-ChildItem -Path $target -depth $depth -recurse
} else {
$folders = Get-ChildItem -Path $target -Directory -depth $depth -recurse
}
foreach ($folder in $folders) {
$acls = Get-Acl -Path $folder.FullName
if ($acls.AreAccessRulesProtected -eq $true) { # we found a folder that does not inherit permissions.
if ($grant -eq '') {
$folder.FullName # This one
$acls | select-object -ExpandProperty access | format-table -Property IdentityReference, AccessControlType, FileSystemRights, IsInherited
} else {
'icacls "{0}" /grant "{1}"' -f $folder.FullName, $grant
}
}
else {
# look for additional acls that were added to the ones inherit3ed from parent folder.
$unique = $acls | select-object -ExpandProperty access | where-Object -property IsInherited -eq $false
if (($unique -ne $null) -and ($grant -eq '')) {
"*{0} (In addition to inherited perms)" -f $folder.FullName # This one
$unique | format-table -Property IdentityReference, AccessControlType, FileSystemRights, IsInherited
}
}
}