Script for Listing Shares

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Script for Listing Shares

#1 Post by mirrormirror » 11 Mar 2016 18:52

Does anyone have some pre-built code they'd be willing to share that lists SHARES with associated permissions to those shares? I currently create a temporary .vbs script using this code to get share names (then delete the .vbs file):

Code: Select all

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    ^& "{impersonationLevel=impersonate}!\\" ^& strComputer ^& "\root\cimv2")
Set colShares = objWMIService.ExecQuery("Select * from Win32_Share")
For each objShare in colShares
    Wscript.Echo objShare.Name ^& "/" ^& objShare.Path
Next

Then use this code (plus the RMTShare.exe" utility) to get permissions:

Code: Select all

   SetLocal EnableDelayedExpansion
   SET "buf=     . . . "
   REM FIX this to SORT output
   FOR /F "usebackq tokens=1,* delims=/" %%a IN (`cscript //nologo "%ListNetShares%"`) DO (
      @ECHO %%a %buf% %%b
      SET "foundPrivs="
      FOR /F "usebackq tokens=*" %%d IN (`RMTShare.exe \\%COMPUTERNAME%\"%%~a"`) DO (
         IF DEFINED foundPrivs (
            IF /I "%%d" neq "The command completed successfully." (
               SET myGrp=%%d
               @ECHO %buf% !myGrp:%COMPUTERNAME%\=!
            )
         )
         IF /I "%%d"=="Permissions:" SET "foundPrivs=1"
         IF /I "%%d"=="No permissions specified." @ECHO %buf% %%d
      )
   )
   EndLocal

I noticed this code from b0gus here viewtopic.php?f=3&t=7011#p45720

Code: Select all

@echo off
setlocal
echo:...wait
for %%v in (wmic.exe) do if "%%~$path:v"=="" (echo:OS missing wmic.exe&pause&exit /b 1)
set "Name="
for /f %%v in ('2^>nul wmic.exe path Win32_Group where SID^="S-1-5-32-544" get name /Format:value^|more +2') do for /f "delims=" %%n in ("%%v") do (set %%n)
if NOT defined Name echo:OS missing group with SID=S-1-5-32-544&pause&exit /b 2
set "admins=" & set "x="
for /f "tokens=*" %%v in ('net localgroup %Name%') do call set "admins=%%admins%% "%%x%%"" & set "x=%%v"
cls
echo:users from localgroup "%Name%":
for %%v in (%admins:*-" =%) do echo:    %%~v
pause
exit /b 0

It seemed pretty robust and the use of WMIC made me think you could do something similar with SHARES in addition to USERS or GROUPS.
So this is NOT a critical need for me - my current solution works and I wouldn't want anyone spending a lot of time working up a new solution. I'm just in learning mode now and looking to see how others have solved these issues to learn new techniques, etc.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Script for Listing Shares

#2 Post by aGerman » 12 Mar 2016 06:33

Try this code to get name and path of the shares.

Code: Select all

for /f "delims=" %%I in ('wmic share get name^,path /format:csv') do (
  for /f "tokens=2* delims=," %%a in ("%%I") do if "%%a" neq "Name" (
    echo %%a     . . . %%b
  )
)


I didn't skip the head line because I remember that the number of empty lines may differ. Thus I rather used an additional if statement (just in case somebody is wondering ...).

Regards
aGerman

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Re: Script for Listing Shares

#3 Post by mirrormirror » 12 Mar 2016 19:24

Try this code to get name and path of the shares.

Thank you. If you have any spare code laying around that enumerates the permissions for each share in XP I'll also use it next time I work on this code.
the "net share [sharename]" command lists permissions in Win7 but not XP, so I am forced to use the "rmtshare.exe" utility to do this. It would be nice to get rid of that dependency. But again, this isn't an urgent need, just curious and looking for other solutions - and I don't have a ton of time to research them at the moment.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Script for Listing Shares

#4 Post by foxidrive » 13 Mar 2016 06:22

A module exists here, including for XP, using powershell to ease file permissions and security.


File System Security PowerShell Module 4.2.1
Allows a much easier management of permissions on files and folders using PowerShell



https://gallery.technet.microsoft.com/s ... dbb2b84e85

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Re: Script for Listing Shares

#5 Post by mirrormirror » 14 Mar 2016 00:06

A module exists here, including for XP, using powershell to ease file permissions and security.

Thanks for the link but unfortunately, the XP environment doesn't have .net installed so I don't think powershell is an option.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Script for Listing Shares

#6 Post by foxidrive » 14 Mar 2016 01:39

mirrormirror wrote:the XP environment doesn't have .net installed


Ok. Who are you supporting to cater for XP? I assumed it was for your own needs.

There are 15 year old kids walking around who are older than XP. :D

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Re: Script for Listing Shares

#7 Post by mirrormirror » 14 Mar 2016 16:24

Ok. Who are you supporting to cater for XP? I assumed it was for your own needs.

There are 15 year old kids walking around who are older than XP. :D

What, you aren't using XP??? :) Anyway, it is for my own use and this particular system is a no-frills box, thus no .net.
The newer microsoft OS's are too invasive in their digital data accumulation/surveillance for my liking. Most people don't know or don't care but that is a separate topic.

Post Reply