RSS

VBScript to list shares and permissions in Windows OS

27 Feb

I thought that this would be an easy script to find, but surprising there are no complete script to be found. I found the following powershell script in the respository

http://gallery.technet.microsoft.com/scriptcenter/List-Share-Permissions-83f8c419

It works and lists the shares, but it does not product the path of the share!

Found another helpful posts for VBScript version here:

http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/55acaaf8-efc5-4432-8ac7-b37eed81defd/

It works but for some reasons the security permissions are not correct

The closest I find is this:

http://www.visualbasicscript.com/Enumerating-Share-Permissions-not-NTFS-m58860.aspx

Works well BUT it again does not list the path of share. Why in the world would I not want to know where a share path is?

So I have to clobber the last 2 VBScript together to produce a functional one that I need. Credits to the authors in the link above:

Dim strComputer : strComputer = "."

Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\" & _
strComputer & "\root\cimv2")

Dim colItems : Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalShareSecuritySetting")

Dim objItem, intRtn, wmiSecurityDescriptor

Dim colDACLs, objACE, objUserGroup, strPermission, colShr, objShr

For Each objItem In colItems
WScript.Echo "Share Name:  " & objItem.Name

Set colShr = objWMIService.ExecQuery("Select * from win32_share where name='" & objItem.Name & "'")
For Each objShr in colShr
  WScript.Echo "Share Path:  " & objShr.Path
Next

intRtn = objItem.GetSecurityDescriptor(wmiSecurityDescriptor)

colDACLs = wmiSecurityDescriptor.DACL

For Each objACE In colDACLs
Set objUserGroup = objACE.Trustee

WScript.Echo vbTab & "User/Group that has access:  " & UCase(objUserGroup.Name)

Select Case objACE.AccessMask
Case 1179817 strPermission = "READ"
Case 1245631 strPermission = "CHANGE"
Case 2032127 strPermission = "FULL CONTROL"
End Select

WScript.Echo vbTab & "Permission:  " & strPermission & VbCrLf

Next

Next

 

Advertisements
 
3 Comments

Posted by on February 27, 2013 in Scripts, VBScripts, Windows

 

Tags:

3 responses to “VBScript to list shares and permissions in Windows OS

  1. Nicole Welch

    August 6, 2013 at 9:29 pm

    Updated to write out to a text file.

    Const ForAppending = 2

    strFile = “.\EnumShares.txt”
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objTextFile = objFSO.OpenTextFile(strFile, ForAppending, True)

    Dim strComputer : strComputer = “.”

    Dim objWMIService : Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

    Dim colItems : Set colItems = objWMIService.ExecQuery(“SELECT * FROM Win32_LogicalShareSecuritySetting”)

    Dim objItem, intRtn, wmiSecurityDescriptor

    Dim colDACLs, objACE, objUserGroup, strPermission, colShr, objShr

    For Each objItem In colItems
    WScript.Echo “Share Name: ” & objItem.Name
    objTextFile.Write objItem.Name

    Set colShr = objWMIService.ExecQuery(“Select * from win32_share where name='” & objItem.Name & “‘”)
    For Each objShr in colShr
    ‘WScript.Echo “Share Path: ” & objShr.Path
    objTextFile.Write vbTab & objShr.Path
    Next

    intRtn = objItem.GetSecurityDescriptor(wmiSecurityDescriptor)

    colDACLs = wmiSecurityDescriptor.DACL

    For Each objACE In colDACLs
    Set objUserGroup = objACE.Trustee

    ‘WScript.Echo vbTab & “User/Group that has access: ” & UCase(objUserGroup.Name)
    objTextFile.Write vbTab & objUserGroup.Name

    Select Case objACE.AccessMask
    Case 1179817 strPermission = “READ”
    Case 1245631 strPermission = “CHANGE”
    Case 2032127 strPermission = “FULL CONTROL”
    End Select

    ‘WScript.Echo vbTab & “Permission: ” & strPermission & VbCrLf
    objTextFile.Write “:” & strPermission

    Next
    objTextFile.writeline
    Next

    objTextFile.Close

     
  2. Toky

    May 24, 2016 at 7:43 pm

    Thank you! very usefull from older wins 😉

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: