Avatar of carbonbase
carbonbase
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Cannot modify permissions on files

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()
	}
}

Open in new window

Powershell

Avatar of undefined
Last Comment
carbonbase

8/22/2022 - Mon
65td

Who owns the files?
ASKER CERTIFIED SOLUTION
carbonbase

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61