?
Solved

Need assistance in setting NTFS folder permissions with VBScript

Posted on 2008-11-12
7
Medium Priority
?
318 Views
Last Modified: 2012-05-05
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
Comment
Question by:mcpp661
7 Comments
 
LVL 7

Expert Comment

by:aherps
ID: 22945861
0
 
LVL 23

Accepted Solution

by:
Malli Boppe earned 1000 total points
ID: 22945996
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
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 1000 total points
ID: 22946342
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.

 

Author Comment

by:mcpp661
ID: 22947084
Is CACLS something that's included with Windows or is it an executable that needs downloaded?
0
 
LVL 23

Expert Comment

by:Malli Boppe
ID: 22947090
No its included in the windows.
0
 

Author Comment

by:mcpp661
ID: 22947117
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
 
LVL 23

Expert Comment

by:Malli Boppe
ID: 22947144
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

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question