Active Directory and WMI: VBscript to enumerate the ntfs rights of a given UNC path and a given level of subfolders

Posted October 22nd, 2010 in ntfsrights by dirk adamsky

Today I have extended the previous script:
it now also enumerates the NTFS rights of subfolders below the share.
As a bonus the level of subfolders can be set

Follow the next steps to run the script (admin rights needed for the WMI connection):

  • copy and paste the script in your favorite text editor
  • save the script (for example c:\temp\uncacl2.vbs)
  • open a command prompt
  • go to “c:\temp”
  • give “cscript uncacl2.vbs” (without quotes) and enter

The script:

' Name : uncacl2.vbs
' Description : script to enumerate the ntfs rights of a given UNC path and a given level of subfolders
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 22-10-2010

strUNCPathName = InputBox("please supply the UNC path to the shared folder")
strSubfolderLevel = InputBox("please supply the subfolder depth (1,2,etc.)")
arrUNC = split(strUNCPathName,"\")
If Ubound(arrUNC) > 3 Then
strRightPartOfPath = Mid(strUNCPathName,(Instr(strUNCPathName,arrUNC(4)) -1))
End If
Set objWMI = GetObject("winmgmts:\\" & arrUNC(2) & "\root\CIMV2")
Set objFileShare = objWMI.Get("Win32_Share.Name=""" & arrUNC(3) & """")
If Right(arrUNC(3),1) = "$" And Len(arrUNC(3)) = 2 Then
strPath = objFileShare.Path & Mid(strRightPartOfPath,2)
Else
strPath = objFileShare.Path & strRightPartOfPath
End If
ShowACL strPath
ViewSubFolders strPath, strSubfolderLevel
Set objFileShare = Nothing
Set objWMI = Nothing

Function ViewSubfolders(strFolder, strMaxlevel)
Set colSubfolders = objWMI.ExecQuery("Associators Of {Win32_Directory.Name='" & strFolder & "'} " &_
"Where AssocClass = Win32_Subdirectory ResultRole = PartComponent")
If strMaxlevel >= 1 Then
    For Each SubFolder in colSubfolders
        wscript.echo SubFolder.Name
        ShowACL SubFolder.Name
        ViewSubFolders SubFolder.Name, (strMaxlevel - 1)
    Next
End If
Set colSubfolders = Nothing
End Function

Function ShowACL(strDir)
Set objFolderSecuritySettings = objWMI.Get("Win32_LogicalFileSecuritySetting.Path='" & strDir & "'")
objFolderSecuritySettings.GetSecurityDescriptor objSD
For Each objAce in objSD.DACL
    Select Case objAce.AccessMask
        Case 1179817
            strRights = "read-only"
        Case 2032127
            strRights = "full-control"
        Case 1245631
            strRights = "change"
    End Select
Wscript.Echo strUNCPathName & " ; " & strDir & " ; " & objAce.Trustee.Domain & " ; " & objAce.Trustee.Name & " ; " & strRights
Next
Set objSD = Nothing
Set objFolderSecuritySettings = Nothing
End Function

When you have problems/questions please post a reply or give a ‘star’ rating.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

Filesystem: VBscript to enumerate the subfolders (recursive) with depth limit

Posted March 5th, 2010 in folders by dirk adamsky

Here’s the second script of today.
It is a mutation of the previous script.
Added is a depth limit, this is done by a second argument of the subroutine.
The subroutine needs two arguments: the path of the folder and the depth level.
The path can be local (“c:temp”) or an UNC path like “\servertest”.

Follow the next steps to run the script (admin rights needed on the folder you want to enumerate):

  • open your favorite text editor
  • copy and paste the script into the editor
  • change the path of the folder to your folder path
  • change the depth value to your depth value (current value = 2)
  • save the script (for example c:tempenumeratesubfolderslevel.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript enumeratesubfolderslevel.vbs” (without quotes) and enter

The script:

' Name : enumeratesubfolderslevel.vbs
' Description : script to enumerate the subfolders (recursive) with depth limit
' Author : dirk adamsky - deludi bv
' Version : 1.01 corrected small counter error
' Date : 05-03-2010
' Level : Intermediate

ViewSubFolders "c:temp", 2
Sub ViewSubFolders(strFolder, strMaxLevel)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFolder = objFSO.GetFolder(strFolder)
	If strMaxlevel >= 1 Then
		For Each SubFolder in objFolder.SubFolders
			Wscript.Echo SubFolder.Path
			ViewSubFolders SubFolder, (strMaxlevel - 1)
		Next
	End If
	Set objFolder = Nothing
	Set objFSO = Nothing
End Sub

When you have problems/questions please make a reply, when you like the script please rate it with the star rating below.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

[adrotate group="2" banner="3"]

Filesystem: VBscript to enumerate all subfolders (recursive) of a given folder

Posted March 5th, 2010 in folders by dirk adamsky

The motto of the day is: Recursion rules.
A couple of years ago I have done quite a few Filesystem Object scripts.
To refresh my mind I have made some new filesystem scripts.
This is the first one: a simple subroutine that uses recursion to enumrate all subfolders of a folder.
The subroutine needs one argument: the path of the folder.
The path can be local (“c:temp”) or an UNC path like “\servertest”.

Follow the next steps to run the script (admin rights needed on the folder you want to enumerate):

  • open your favorite text editor
  • copy and paste the script into the editor
  • change the path of the folder to your folder path
  • save the script (for example c:tempenumeratesubfolders.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript enumeratesubfolders.vbs” (without quotes) and enter

The script:

' Name : enumeratesubfolders.vbs
' Description : script to enumerate all subfolders (recursive) of a given folder
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 05-03-2010
' Level : Intermediate

ViewSubFolders "c:temp"
Sub ViewSubFolders(strFolder)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFolder = objFSO.GetFolder(strFolder)
	For Each SubFolder in objFolder.SubFolders
		Wscript.Echo SubFolder.Path
		ViewSubFolders SubFolder
	Next
	Set objFolder = Nothing
	Set objFSO = Nothing
End Sub

When you have problems/questions please make a reply, when you like the script please rate it with the star rating below.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

[adrotate group="3"]