Wednesday, November 5, 2014

Remotely obtain a list of Windows shares, physical Path and Share Permissions in CSV or Excel format

Have you ever needed to present a quick report of a Server's Shares, the Path of each share, and maybe the share permissions assigned to each share?
If that is the case you would probably needed to present it in Excel or similar format.

So Here's a way to do it, copy the next content on a .bat or .cmd:
del shares.txt
for /f "Skip=4 Tokens=1 delims=:" %%p in ('rmtshare \\%servername%^|find /v /i "IPC$"^|find /v /i "The command completed successfully."^|sed "s/[ \t[a-z]]*$//I"') do (echo \\%servername%\%%p>>shares.txt)
sed -i "s/\s*[a-z]*$//I" shares.txt && del sed*.
del shares-with-quotes.txt
sed -i "s/&/^&/g" shares.txt
del sed*.
for /f "tokens=*" %%i in (SHARES.txt) do echo %%i | sed -e "s;\(.*\\\);\1"";" | sed "$s/.$/""/" >>shares-with-quotes.txt

REM lista share permissions

REM ESTA LINEA SIRVE PARA MODELAR EL ARCHIVO INPUT:  for /f "tokens=*" %%i in (SHARES.txt) do echo %%i | sed -e "s;\(.*\\\);\1"";" | sed "$s/.$/""/" >>shares-with-quotes.txt

for /f "tokens=*" %%i in (shares-with-quotes.txt) do (
rmtshare %%i| sed -nE "s/^Share name *(\\\\.*)/\1/p;s/^Path *(.*)$/\1/p;/Permissions:/,/The command completed successfully/{/Permissions:\^^|The command completed successfully/!p}" | sed "{:q;N;s/\n/;/g;t q}"  | sed "s/;\\\\/;\n\\\\/g" | sed "s/;$//" >> rmtshares2.log  2>&1
sed -i "s/Permissions:;//g;s/ *.:. /:/g;s/        \\//g;s/;Permissions://g" rmtshares2.log
move rmtshares2.log LISTASHARE.TXT
del sed*.
The output as it can be see is going to be: LISTASHARE.TXT but rename it as .csv if you prefer.
It will get the list of shares from the server, and will obtain the physical path, and the permissions in CSV format so it is importable into excel and do a TEXT TO COLUMNS using semicolons. 

What executables or binaries do I need?
sed.exe (and its dependencies 3 .DLLs)
rmtshare.exe (Microsoft tool) 

You can get all together including the scripts and an additional script where you just need to fill in specifically a list of shares you want into a file named shares.txt then run otro.cmd
From here: Click here please  (it is a link from adfly, just click on the SKIP AD button which will appear after 5 Seconds). 
Then you can just click on the file which is hosted on 
Important, the zip file has a password to make sure it was not edited or anything like that: 
In this file: you will find:
Content of the

sed.exe and its dependencies 3 DLL files(unix port you may find it 
rmtshare.exe (alternative location you may find it at least for now) 
and Two scripts: 
5.cmd: just edit the file and where it says  Set Servername=type your server name here and save it. For example  Set Servername=FileSERVER1
otro.cmd: for this all what you need to do is to fill in a text file named shares.txt with a list of share (do not leave any space that is not required for example "\\server1\share1 " it should read "\\server1\share1" (without the quotes)).

The result could look similar to this: (open or paste the content of Listashare.txt with excel). 

If you use it leave me a comment, tell me if you liked it. Try testing it as much as possible.

And thanks to my collegue friend Seba Bistakis (he gave me a piece of code with sed.exe to avoid using tr.exe).


Post a Comment