wandersick / ws-folder-template-provisioner

This Windows batch script provisions (copies) new folders with exact permissions and content from a specified existing folder (template) based on the information (first name and last name) inputted by a user via its command-line interface.
https://tech.wandersick.com/2020/09/folder-template-provisioner.html
4 stars 0 forks source link
administration directory file-explorer folder nas ntfs permissions qnap robocopy runas samba shared-folders synology sysadmin template windows windows-admin windows-explorer

Folder Template Provisioner

This Windows batch script provisions (copies) new folders with exact permissions and content from a specified existing folder (template) based on the information inputted by a user via its command-line interface. It hopes to reduce the burden of Windows admins by handling over the task of folder provisioning to users.

It uses robocopy /MIR /COPYALL /ZB (built-in) to solve the problem where folders copied using Windows Explorer (a.k.a. File Explorer) may not retain unique permissions and inherit permissions from parent folder.

What's New

Features

Requirements

  1. Windows OS with robocopy
  2. admin rights (with or without UAC)
  3. Non-admin rights (partially supported with an optional setup on _runasAdmin.bat)

Script Filenames

  1. FolderTemplateProvisioner.bat (main script)
  2. _elevate.vbs (optional, for admin users with UAC turned on, trigger UAC elevation prompt)
  3. _runasAdmin.bat (optional, for non-admin users to leverage runas /savecred to run as admin)

Folder Hierarchy

│ FolderTemplateProvisioner.bat // main script
│ _elevate.vbs // For admin users with UAC enabled to auto-trigger UAC prompt (optional)
│ _runasAdmin.bat // For non-admins to run as admin without admin credentials (optional)
│
├───ZZ IT_do not use\01 Template Folder // Template folder (this can be a sub-folder)
│                                       // with folders/files of set permissions to copy
│
├───A // Last names beginning with an "A"
│   │───LASTNAME, Firstname 1
│   │───LASTNAME, Firstname 2
│   │                       .  // Each copied folder is named in this way according
│   │                       .  // to names inputted by user with sub-folders and files
│   │                       .  // retaining specific permissions
│   └───LASTNAME, Firstname n
│
├───B
├───C
│   .
│   .   // Created folders are placed in folders according to initials from user input
│   .
└───Z

Step by Step - How to Set up the Scripts

Step 1. Skip this step and go straight to step 2 if users have admin rights. This is for cases where users executing the script do not have admin rights.

Edit the UNC path in _runasAdmin.bat setting it to the the script path, e.g. \\serverName\d$\Human Resources\01_Personnel-Files\FolderTemplateProvisioner.bat

Edit /u:DOMAIN\username by changing DOMAIN to the Active Directory domain name (NETBIOS name), and the username as the admin user. For non-domain (workgroup) scenarios, DOMAIN can be skipped by removing DOMAIN\

Before _runasAdmin.bat (or the runas /savecred ... command) can be run by non-admin users, it needs to be run once on PCs of users who need to use Folder Template Provisioner without admin credentials. (The first run involves prompting for admin credentials where an admin needs to be there to input admin password once)

Step 2. Edit encPath variable at the upper area of FolderTemplateProvisioner.bat script by setting it to the network folder containing the script, e.g. \\serverName\d$\Human Resources\01_Personnel-Files

Step 3. Edit templateName variable at the upper area of FolderTemplateProvisioner.bat script by setting it to the folder acting as the template, e.g. ZZ IT_do not use\01 Template Folder, with required files and permissions inside

Step 4. Place all scripts (FolderTemplateProvisioner.bat, optionally _elevate.vbs and _runasAdmin.bat) inside a folder containing A,B...Z sub-folders, sitting aside. The A-Z folders contains the template folder and provisioned folders named LASTNAME, Firstname copied by the script from the template folder

Step by Step - How to Provision a New Folder

Step 1. Double-click FolderTemplateProvisioner.bat and follow on-screen instructions

Step 2. Input last name and first name

Step 3. Review the input

Step 4. Wait for robocopy file copy (folder template provisioning)

Step 5. Verify the created folder

Screenshots - Provisioning a New Folder with the Script

A. Run FolderTemplateProvisioner.bat

Run FolderTemplateProvisioner.bat

Note: non-admins should run _runasAdmin.bat instead, available since v1.2

For admins with UAC enabled, a UAC elevation prompt (like the one below) is displayed automatically, provided that an optional _elevate.vbs exists in the same folder. (_elevate.vbs should not be run directly. Instead, run FolderTemplateProvisioner.bat)

For admins with UAC enabled, UAC elevation prompt is displayed automatically, provided that an optional _elevate.vbs exists in the same folder

B. Input first name and last name

Input first name and last name

C. Review and confirm input

Review and confirm input

Note: Since v1.2, users would receive a message requesting them to ensure no folder of the same name exists in destination before continuing; otherwise, content would be overwritten.

D. Robocopy provisions new folder from folder template with exact content and permissions

Robocopy provisions new folder from folder template with exact content and permissions

Note: Since v1.2, users would receive a message at the end recommending them to verify the created folder at target location.

Screenshots - Error Messages

A. Name inputted by user is invalid

Error - Name inputted by user is invalid

B. Robocopy error ends with a return code presented to user

Error - Robocopy error ends with a return code presented to user

C. No admin rights are detected, while UAC is off and/or the optional _elevate.vbs is not in the same folder

Error - No admin rights are detected, while UAC is off and/or the optional '_elevate.vbs' is not in the same folder

Other error messages include:

Security Note

This part can be skipped if you don't let users use this set of scripts without admin rights.

As with most technology that enables convenience, kindly beware of the natural security risk involved with _runasAdmin.bat, i.e. runas /savecred. In this case, a non-admin user would be able to use the runas /savecred command to run any command as an admin without entering a password. Please do understand such risk involved if you still would like to go ahead with the non-admin approach to using this script.

Another Possible Approach for Non-Admin

This part can be skipped. It is only for enthusiasts wishing to modify this script to run it as non-admin without even using _runasAdmin.bat or runas /savecred.

Several prerequisites and/or adjustments would be required (experimental):

  1. Remove /ZB parameter from the robocopy command in the script (note: /ZB uses backup operator rights to copy if permission is unavailable. This requires admin rights)
  2. Change /COPYALL to /COPY:DATSO (this removes the 'manage auditing' part which requires admin rights)
  3. The template folder should include the desired non-admin user as owner with full permission in the underlying folders
  4. Remove lines in this script between attrib >nul 2>&1 and :skipAdminCheckSubsequent

Note: The above is approach is experimental, hence unsupported.

Release History

Ver Date Changes
1.2 20200730 - Support of network-shared folder in UNC form (\\...) is available
- Support of non-admin users is provided by runas /savecred
- Remove support of UNC-unsupported error-checking cases
1.1 20200719 Folder template consolidated as single folder instead of multiple ones within sub-folders
1.0 20200714 First release where template folders are located in each sub-folders