troubleshooting Question

Cannot modify permissions on files

Avatar of carbonbase
carbonbaseFlag for United Kingdom of Great Britain and Northern Ireland asked on
Powershell
2 Comments1 Solution77 ViewsLast Modified:
Hi I have some Powershell code which in the past I've used to modify permissions on files and folders.  After a number of months I've run the code against some data I've migrated from a decommissioned NAS device.  The problem I'm having is that only permissions on folders have changed, but I want to change both the folder and file permissions.  I'm able to change permissions on folders, even ones I don't own but I cannot change permissions on files.

$rights = [System.Security.AccessControl.FileSystemRights]::Modify
$inheritance = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit,ObjectInherit"
$propagation = [System.Security.AccessControl.PropagationFlags]::None
$allowdeny = [System.Security.AccessControl.AccessControlType]::Allow

$error.Clear()

# Customize the domain name
$myDomain = "mydomain.mycompany.com"
$adminUserName = "IT Dept"
[string]$ErrorLog = 'C:\Temp\Permission_Errors.txt'
$StartFolder = 'V:\Adobe'

$Account = [System.Security.Principal.NTAccount]"$myDomain\$adminUserName"
& cmd.exe /c "echo \\?\$($StartFolder)&dir /s /a:d /b `"\\?\$($StartFolder)`"" | ForEach `
{
	$folder = $_.SubString(4)
	Try {
		$Acl = Get-Acl -LiteralPath $folder -ErrorAction Stop
		$CurrentAccess = @($Acl.Access | Select-Object -ExpandProperty IdentityReference)
		If ($CurrentAccess -Contains $Account)    
		{
			Write-Host  "Skipping folder.  This user account already has permission: $Account"
			Write-Host  "To the folder: $($folder)"
		}       
		Else
		{
			$Ace = New-Object System.Security.AccessControl.FileSystemAccessRule -ArgumentList $Account, $rights, $inheritance, $propagation, $allowdeny
			$Acl.AddAccessRule($Ace)
			Write-Host "Setting permission for folder: $($folder)" -ForegroundColor Yellow
			$error.Clear()
			Set-Acl -LiteralPath $folder -AclObject $Acl -ErrorAction Stop
		} 
	}
	Catch
	{
		Write-Warning "Problem setting permission to folder:  $($folder)"
		Write-Warning "The following error occurred: $error " 
		"Problem getting/setting permission to folder: '$($folder)'.  The Error encountered was: '$error'" | Out-File $ErrorLog -Append
        $error.Clear()
	}
}
ASKER CERTIFIED SOLUTION
carbonbase

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros