One of the tasks that seems to come up fairly regularly when managing school servers is fixing the permissions for home directories. For whatever reason, the permissions for these can get messed up and need a quick tweak to reset them to the default.
This Powershell script does just that. There are a couple of requirements:
- The person running the script needs adequate permissions to edit the permissions for all the directories
- The directories need to be named using the usernames of the owner of the home directories
This code sets domain admin as the owner of the folder, removes the old permissions, then adds the new permissions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$folders = get-childitem 'D:\Users\Redirects' -directory $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None foreach($folder in $folders){ # Skip Pupils subfolder if it exists if($folder.Name -ne "Pupils"){ #Get Current ACL for the folder $Acl = Get-Acl $folder.FullName Write-Host "Setting full permissions for $($folder.Name)" #Create new ACL $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($folder.Name,"FullControl",$InheritanceFlag, $PropagationFlag, "Allow") $Acl.Access | %{$Acl.RemoveAccessRule($_)} $Acl.SetOwner([System.Security.Principal.NTAccount]"Domain Admins"); #Remove existing permissions Set-Acl -Path $folder.FullName -AclObject $Acl $Acl.SetAccessRule($ar) #Add new permissions Set-Acl -Path $folder.FullName -AclObject $Acl } } |
Be First to Comment