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()
}
}
Select all Open in new window