Question : VB script to enble inheritance on folders and files

Hi Experts,
Im looking script to enble inheritance on folders and files

Here is the script that was written by Chris-Dent, however, the script just works on files, so it would be easy for an expert in VBscript to change it to include folders.

http://www.experts-exchange.com/Security/Operating_Systems_Security/Windows/Q_21953774.html#discussion
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
Option Explicit

Const FS_TREE_ROOT = "C:\Temp"

Const SE_DACL_PRESENT = 4
Const SE_DACL_AUTO_INHERITED = 1024
Const SE_DACL_PROTECTED = 4096
Const SE_SELF_RELATIVE = 32768

Const ENABLE_INHERITANCE = 1
Const COPY_AND_TURN_OFF_INHERITANCE = 2
Const REMOVE_AND_TURN_OFF_INHERITANCE = 3

'
' Subroutines
'

Sub ResetInheritance(strPath, intWhatToDoWithInheritance)
      Dim objFileSecSetting, objSecurityDescriptor, objMethod, objInParam
      Dim intControlFlagsVar

      Set objFileSecSetting = objService.Get("Win32_LogicalFileSecuritySetting.Path=""" &_
            Replace(strPath,"\","\\") & """")
      objFileSecSetting.GetSecurityDescriptor objSecurityDescriptor      

      Select Case intWhatToDoWithInheritance
      Case ENABLE_INHERITANCE
            intControlFlagsVar = SE_DACL_PRESENT + SE_DACL_AUTO_INHERITED + SE_SELF_RELATIVE
      Case COPY_AND_TURN_OFF_INHERITANCE, REMOVE_AND_TURN_OFF_INHERITANCE
            intControlFlagsVar = SE_DACL_PRESENT + SE_DACL_PROTECTED + SE_SELF_RELATIVE      
      End Select
      
      objSecurityDescriptor.ControlFlags = intControlFlagsVar
            
      Set objMethod = objFileSecSetting.Methods_("SetSecurityDescriptor")
      Set objInParam = objMethod.inParameters.SpawnInstance_()
      objInParam.Properties_.item("Descriptor") = objSecurityDescriptor
      
      objFileSecSetting.ExecMethod_ "SetSecurityDescriptor", objInParam
End Sub

Sub DoWorkOnFolders(strPath)
      Dim objWorkingFolder, objWorkingFile, objWorkingSubFolder

      Set objWorkingFolder = objFileSystem.GetFolder(strPath)

      WScript.Echo "Working on " & strPath
      
      For Each objWorkingFile in objWorkingFolder.Files
            ResetInheritance objWorkingFile.Path, ENABLE_INHERITANCE
      Next

      For Each objWorkingSubFolder in objWorkingFolder.SubFolders
            DoWorkOnFolders objWorkingSubFolder.Path
      Next
End Sub

'
' Main Code
'

Dim objFileSystem, objLocator, objService, objRootFolder, objFile, objFolder

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer (".", "root/cimv2")

Set objRootFolder = objFileSystem.GetFolder(FS_TREE_ROOT)

For Each objFile in objRootFolder.Files
      ResetInheritance objFile.Path, ENABLE_INHERITANCE
Next      

Set objFile = Nothing

For Each objFolder in objRootFolder.SubFolders
      DoWorkOnFolders objFolder.Path
Next

Set objFolder = Nothing

Set objRootFolder = Nothing
Set objService = Nothing
Set objLocator = Nothing
Set objFileSystem = Nothing

Answer : VB script to enble inheritance on folders and files


Hmm... try this?

Chris
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
Option Explicit

Const FS_TREE_ROOT = "C:\Temp"

Const SE_DACL_PRESENT = 4
Const SE_DACL_AUTO_INHERITED = 1024
Const SE_DACL_PROTECTED = 4096
Const SE_SELF_RELATIVE = 32768

Const ENABLE_INHERITANCE = 1
Const COPY_AND_TURN_OFF_INHERITANCE = 2
Const REMOVE_AND_TURN_OFF_INHERITANCE = 3

'
' Subroutines
'

Sub ResetInheritance(strPath, intWhatToDoWithInheritance)
      Dim objFileSecSetting, objSecurityDescriptor, objMethod, objInParam
      Dim intControlFlagsVar

      Set objFileSecSetting = objService.Get("Win32_LogicalFileSecuritySetting.Path=""" &_
            Replace(strPath,"\","\\") & """")
      objFileSecSetting.GetSecurityDescriptor objSecurityDescriptor      

      Select Case intWhatToDoWithInheritance
      Case ENABLE_INHERITANCE
            intControlFlagsVar = SE_DACL_PRESENT + SE_DACL_AUTO_INHERITED + SE_SELF_RELATIVE
      Case COPY_AND_TURN_OFF_INHERITANCE, REMOVE_AND_TURN_OFF_INHERITANCE
            intControlFlagsVar = SE_DACL_PRESENT + SE_DACL_PROTECTED + SE_SELF_RELATIVE      
      End Select
      
      objSecurityDescriptor.ControlFlags = intControlFlagsVar
            
      Set objMethod = objFileSecSetting.Methods_("SetSecurityDescriptor")
      Set objInParam = objMethod.inParameters.SpawnInstance_()
      objInParam.Properties_.item("Descriptor") = objSecurityDescriptor
      
      objFileSecSetting.ExecMethod_ "SetSecurityDescriptor", objInParam
End Sub

Sub DoWorkOnFolders(strPath)
      Dim objWorkingFolder, objWorkingFile, objWorkingSubFolder

      ' Reset folder level inheritance
      ResetInheritance strPath, ENABLE_INHERITANCE

      Set objWorkingFolder = objFileSystem.GetFolder(strPath)

      WScript.Echo "Working on " & strPath
      
      For Each objWorkingFile in objWorkingFolder.Files
            ResetInheritance objWorkingFile.Path, ENABLE_INHERITANCE
      Next

      For Each objWorkingSubFolder in objWorkingFolder.SubFolders
            DoWorkOnFolders objWorkingSubFolder.Path
      Next
End Sub

'
' Main Code
'

Dim objFileSystem, objLocator, objService, objRootFolder, objFile, objFolder

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer (".", "root/cimv2")

Set objRootFolder = objFileSystem.GetFolder(FS_TREE_ROOT)

For Each objFile in objRootFolder.Files
      ResetInheritance objFile.Path, ENABLE_INHERITANCE
Next      

Set objFile = Nothing

For Each objFolder in objRootFolder.SubFolders
      DoWorkOnFolders objFolder.Path
Next

Set objFolder = Nothing

Set objRootFolder = Nothing
Set objService = Nothing
Set objLocator = Nothing
Set objFileSystem = Nothing
Random Solutions  
 
programming4us programming4us