Wednesday, June 4, 2014

How to Reset Redirected Folder NTFS Permissions with PowerShell

I have been dealing with user redirected folders ever since Windows Server 2000. This is a great feature as many of you know, especially in Citrix environments where you do not want to keep data on XenApp servers or read-only images. Generally, you set it up "correctly" and it usually just works as long as you don't touch it much. Then you start to get requests to get into the users folders and you need to do so as an administrator, or you are wanting fix the security settings because someone has modified the NTFS permissions for several users to gain access, but failed to properly set the permissions back to the way it needs to be based on what Microsoft recommends. To fix a redirected folder store for a few users, not a big deal. But, to fix a redirected folder store for many users, you can imagine the millions of clicks that have to be done which usually scares people off.

A couple of years ago, I had to fix permissions on a clients redirected folder store which contained about 1000 users. Instead of wasting my time and the clients money, I decided to create a script that would do what I needed. After I built, this script, we ran it against the clients environment and everything went as planned. All permissions were fixed as the customer request of adding administrator groups to the existing store so that they would not need to modify permissions to get into users folders which caused the problem in the first place. This script (and modifications of this script for various clients requirements) have been successfully run in many environments, which in turn has saved countless hours and money.

Before I go over the script instructions, I want to go over the permissions as recommended by Microsoft that the script modifies (Note: This script does not modify share permissions):


  • NTFS permissions for the root folder:
    • Creator Owner - Full Control, Subfolders and Files Only
    • Administrator - None (the script will give you the option to modify this to allow full rights to administrators)
    • Security group of users that will be using folder redirection - List Folder / Read Data, Create Folders / Append Data - This Folder Only
    • Local System - Full Control, This Folder, Subfolders and Files
  • NTFS permissions for the users' redirected folders:
    • %Username% - Full Control, Owner of Folder
    • Local System - Full Control
    • Administrators - None (the script will give you the option to modify this to allow full rights to administrators)
To successfully run this script, the following is required:
  • Before doing anything, backup your data. I take no responsibility of this script when you run it in your own environment.
  • Test the script in your environment, take a snap shot of the data and verify the results are what you want before running the script on your production data.
  • Takeown.exe is required as part of the script. This will probably already be on the system that will run the script, but just make sure.
  • The domain name of the domain you will be running this script in (e.g. domain.local)
  • A UNC path to the redirected folders (e.g. \\server\redirfolders)
  • An Active Directory administrative security group. This group is used as part of the process to reset the permissions as well as if you want to have administrative access, this group will be left, otherwise it will be removed once the permissions are set.
  • An Active Directory user security group that is using the redirected folder feature.
  • PowerShell run with administrative rights.
  • The user that will be launching this script to be part of the administrative group mentioned above.
  • For better performance, disable antivirus when running this script.
  • Make sure to disable any replication that may be setup for this data while you run this script (please note, if you are using DFSR, this will cause the algorithm used by DFSR see the files and folders as updated and copy according to your DFSR configuration).
  • Do not run this script while users are accessing the system.
  • Did I mention to backup prior to running the script.
  • When running the script against the test data, make note of the amount of data as well as how long the script took to process the data to help make a calculation of how long the script will take on your production data.
To run the script:
  1. Download and place it in a directory of your choosing. It is recommended to run this on the same server as where the files reside.
  2. Open PowerShell as an administrator.
  3. Since this is really a PowerShell function, you need to add the function to your PowerShell session with the following dot sourcing:
    1. ". ./set-redirectedfolderpermissions.ps1"
    2. Do not include the "
    3. There is a space between the 2 periods in the beginning
  4. Once you dot source the script which now adds the function to use in your PowerShell session, to run the function:
    1. set-redirectedfolderpermissions
    2. If you use auto complete, make sure to remove the .ps1 at the end.
  5. You will be prompted for the information mentioned above in the requirements:
    1. DomainName - Enter the domain name (e.g. domain.local).
    2. RootProfileUNCDir - Enter the UNC path to the root folder (e.g. \\server\redirfolders).
    3. AdminGroup - Enter the admin group that will be used to set the appropriate permissions as well as which group you want to assign administrative rights to the users folders (e.g. Domain Admins).
    4. UserGroup - Enter the user group that uses redirected folders (e.g. Domain Users).
    5. GiveAdminAccess - Specified in either Y or N to allow the administrators group access to users folders (e.g. Y)
    6. GiveLocalAdminAccess - Specified in either Y or N to allow local administrators rights to users folders (e.g. N)
  6. After you enter the above information, you will be asked to confirm if everything is correct, use Y or N to confirm.
  7. The script will run, and depending on the amount of data, it might run for a long time. You will see output in the PowerShell screen of what is happening.
  8. In some instances, the script might have to run a couple of times to get everything set correctly.
To download the script, please go to my scripts page or click here.

Please let me know what you think, or if you have any questions or issues.


8 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Trying to run script, never prompts me for information

    ReplyDelete
    Replies
    1. Were there any errors when you ran the following:

      3. Since this is really a PowerShell function, you need to add the function to your PowerShell session with the following dot sourcing:
      1. ". ./set-redirectedfolderpermissions.ps1"
      2. Do not include the "
      3. There is a space between the 2 periods in the beginning
      4. Once you dot source the script which now adds the function to use in your PowerShell session, to run the function:
      1. ./set-redirectedfolderpermissions
      2. If you use auto complete, make sure to remove the .ps1 at the end.

      Delete
    2. I too am not able to run the script. Commands run and nothing happens.

      Delete
    3. I apologize, there was a typo in the directions under step #4. This has been corrected. After you dot source the script, all you need to type is set-redirectedfolderpermissions.

      Delete
  3. This saved me hours of work. You're a gentleman for sharing this!

    ReplyDelete
  4. Hi Chris, Nice script... in my job we occasionally see admins mess up setting up a users data on redirection by manually copying some bits over in there admin account context rather than leaving redirection to automatically establish, this leaves the user not being able to see their own data and redirection not being able to fix the permissions issue itself. I'm modifying your script somewhat to repair a single users redirected folder only as I don't see the necessity to reset all the permissions (6000+ users)
    There has got to be a way to do the take ownership without the dependency on that exe.???

    thanks!!!

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete