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
|