Monday, 14 August 2017

Simple Batch Script to Share and setup Share permission for Multiple Folders

Hello Everyone,

We have recently received request to share and set share permission for about 500 users home directories.

Initially, we did not thought to script it and start working to share them manually :( .  I know it was bad idea to share all the folders and set share permission manually.

Later, we thought avoid this manual work again , just in case, if we get the similar request in future. So, I have started to work on the batch scripting with "NET SHARE"  command.

Overview of Script as snapshot :





Script details :

1) Prepare “ShareFolderList.txt” input file in the c:\temp directory. Sample file is attached for reference 
Sample file : 


2) Create new File “FolderSharing.bat” in the c:\temp directory, copy the content below and save it as (.bat) file


REM ******** Start of Script *******

REM "To Share the list of folders in the input file"
REM  This script is created to share the adagility.net cluster P:Homedirectories

REM Author: Murugan Natarajan / http://techmurugan.blogspot.in/
REM Created on: 24/7/2017
REM ------------------------------------------------------------------------------------------

REM Error checking for the input file for looping


if not exist "c:\temp\ShareFolderList.txt" GOTO Error

REM if input file exist then process the command to share the folders and set share permission

If exist "c:\temp\ShareFolderList.txt" GOTO LineExecute1

:LineExecute1
 
for /F %%a in (c:\temp\ShareFolderlist.txt) do NET SHARE "%%a$"="D:\HomeDIRs\%%a"  /Grant:Everyone,Change /Grant:Administrators,Full

goto END

:Error
Echo "There is no ShareFolderList.txt file available at c:\temp path"

:END

REM   *****  END of Script ******


Note: I have add error line , just to make sure that input file is created before running the batch file. If there is no c:\temp\ShareFolderList.txt file in the path, then script will END and it will not process the "NET SHARE" command.

3) Open command prompt “Runas Administrator – if needed ”
4) In the c:\temp directory, Type “FolderSharing.bat” (without quotes) and press enter
5) Now, script runs in the command prompt with processing results in the console.

Note: For testing purpose, I suggest to try with only two folders and check the sharing and share permission. If all working without any issues, we can add more folder names to the “ShareFolderlist.txt” input file and excute the FolderSharing.bat file again.

To verify the share is created or not , we can use the command “Net share” in the command prompt window.

 

No comments:

Post a Comment