I've been working on the script to do this for a while now and it is now a combination of three scripts.
The first script "shareallsubfolders.vbs" that I run on the root of the folder with all of the users home directories in it, will declare the variables, perform validation, and then call the other two scripts while passing the appropriate arguments to them.
The second script "setsharepermissions.bat" will create the hidden share, and set the appropriate share permissions on the folder.
The third script "setsecpermissions.vbs" sets the appropriate NTFS permissions on the share.
I then map the users profile's to \\server\%username%$.
The scripts seem to be working correctly except the "setsharepermissions.bat" which will not allow me to give domain admins full control on the folder. I believe that it has something to do with the space in the group name. If anybody has any suggestions on the proper syntax for this please help.
Thanks,
*shareallsubfolders.vbs
set objFSO = createobject("Scripting.FileSystemObject")
On Error Resume Next
RootFolder = inputbox("please enter the root of the partent folder")
'***** Perform some basic validation *****
if not objFSO.FolderExists(RootFolder) then
wscript.echo "Invalid Folder"
wscript.quit
end if
if mid(RootFolder,1,2)="\\" then
wscript.echo "UNC Paths are not supported"
wscript.quit
end if
set objRootFolder = objFSO.GetFolder(rootfolder)
for each fldr in objRootFolder.SubFolders
sharefolder fldr.path, fldr.name
next
wscript.echo "All folders have been shared"
sub shareFolder(byval folderPath,shareName)
'get the name of the folder to resolve the username
dim strUser
strUser = "DOMAINNAME\" & shareName
'use a $ to hide the share
shareName = shareName & "$"
'Call other two scripts to set share and NTFS permissions
Dim objShell
Set objShell = CreateObject("wscript.Shell")
objShell.Run ("D:\scripts\setsharepermissions.bat " & shareName & " " & folderPath & " " & strUser)
objShell.Run ("D:\scripts\setsecpermissions.vbs " & folderPath & " " & strUser)
'wscript.echo (shareName & "has been shared with the correct permissions")
end sub
*setsharepermissions.bat
echo off
REM command shares a given folder with a given share name and grants domain administrators full control for share permissions.
REM %1 is the first argument or sharname
REM %2 is the second argument or folder path.
REM Note that this does not set NTFS permissions
NET SHARE %1=%2 /remark:"Shared by Remote Command" /GRANT:"DOMAIN\Domain admins", FULL /GRANT:"%3",FULL
*setsecpermissions.vbs
Option Explicit
Dim strHomeFolder, strHome, strUserName
Dim intRunError, objShell, objFSO, args
Set objShell = CreateObject("Wscript.Shell")
Set args = WScript.Arguments
'Argument 1 is the home folder
strHomeFolder = args.Item(0)
'Argument 2 is the UserName
strUserName = args.item(1)
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strHomeFolder) Then
'Assign NTFS permissions to folder.
intRunError = objShell.Run("%COMSPEC% /C Echo Y| cacls " & strHomeFolder & " /T /C /G ""Domain Admins"":F " & strUserName &":F", 2, True)
If intRunError <> 0 Then
Wscript.Echo "Error assigning permissions for user " & strUserName & " to home folder " & strHomeFolder
End If
End If
WScript.Quit