• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

Need assistance in setting NTFS folder permissions with VBScript

I have the code below which I found on this and other Web sites and customized it to my needs. It creates a folder on a file server and shares it. However, the last thing I need to do is to add a specific user to the NTFS permissions and assign modify permissions. I want it to retain inherited permissions, but just want to add a user to the ACL and give him/her modify permissions. What is the best way to go about this? Thank you.
Option Explicit
'On Error Resume Next
Dim objFSO, objExcel, objSpread
Dim objNewShare, objWMIService, objUser
Dim strParentFolder, strSheet, strSam, intRow, strFirst, strLast
Dim strShareName, errReturn, strPathName, strComputer, strSharePath
 
Const FS = 0
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strSheet = "c:\UsersWithHomeFolders.xls"
strComputer = "server"
strParentFolder = "\\" & strComputer & "\e$\Files\Users\"
 
' Check to see if spreadsheet exits
If objFSO.FileExists(strSheet) = False Then
  MsgBox("The file " & strSheet & " does not exist. Please create the file and run the script again.")
  WScript.Quit
End If
 
' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSpread = objExcel.Workbooks.Open(strSheet)
intRow = 3 'Row 1 often contains headings
 
' Here is the 'DO...Loop' that cycles through the cells
' Note intRow, x must correspond to the column in strSheet
' Trim function eliminates blank spaces
Do Until objExcel.Cells(intRow,1).Value = ""
   strSam = Trim(objExcel.Cells(intRow, 1).Value)
   strLast = Trim(objExcel.Cells(intRow, 3).Value)
   strFirst = Trim(objExcel.Cells(intRow, 4).Value)
   strPathName = strParentFolder & strLast & ", " & strFirst
   strShareName = strSam & "$"
   strSharePath = "e:\Files\Users\" & strLast & ", " & strFirst
 
   If objFSO.FolderExists(strPathName) = False Then
      objFSO.CreateFolder strPathName 
     'this code creates the share
     Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
     Set objNewShare = objWMIService.Get("Win32_Share")
     errReturn = objNewShare.Create(strSharePath, strShareName, FS)
   End If
 
   intRow = intRow + 1
   
Loop
 
objExcel.Quit
 
Set objFSO = Nothing
Set objExcel = Nothing
Set objSpread = Nothing
Set objWMIService = Nothing
Set objNewShare = Nothing
 
WScript.Quit

Open in new window

0
mcpp661
Asked:
mcpp661
2 Solutions
 
aherpsCommented:
0
 
Malli BoppeCommented:
You can use cacls to set permissions.
So you can write a batch script which calls this vbscript and next line you would be cacls which would set the permissions or you can run the cacls in the vbscript as well.
0
 
sirbountyCommented:
Modify this section of your code with the attached:

   If objFSO.FolderExists(strPathName) = False Then
      objFSO.CreateFolder strPathName
     'this code creates the share
     Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
     Set objNewShare = objWMIService.Get("Win32_Share")
     errReturn = objNewShare.Create(strSharePath, strShareName, FS)
   End If

   If objFSO.FolderExists(strPathName) = False Then
      objFSO.CreateFolder strPathName 
     'this code creates the share
     Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
     Set objNewShare = objWMIService.Get("Win32_Share")
     errReturn = objNewShare.Create(strSharePath, strShareName, FS)
     Dim objShell : Set objShell = CreateObject("Wscript.Shell")
     objShell.Run "cacls " & strSharePath & " /e /t /g YOURUSER:C"
   End If
 
Replace YOURUSER with the user account...

Open in new window

0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
mcpp661Author Commented:
Is CACLS something that's included with Windows or is it an executable that needs downloaded?
0
 
Malli BoppeCommented:
No its included in the windows.
0
 
mcpp661Author Commented:
Well, in the script file do I have to include the path to CACLS? If so, do you know what the path is off the top of your head? Also, the folder permissions I want to set are on a file server but I'm going to run this script from my local workstation, does that mean the objshell.run line will have to be modified to reflect that?
0
 
Malli BoppeCommented:
The file is located in c:\windows\syatem32 so you don't need to specify the path.You need to modify the objshell.run ans speciify the UNC path of your file server.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now