Skip navigation

JSI Tip 9475. How can I list the objects that a security pricipal owns on a specified share?


Using WhoOwns.bat from How can I get the DIR command to return a file's owner in a batch script, I have scripted SFOwner.bat to report the file system objects owned by a specified security principal on a remote share.

The syntax for using SFOwner.bat is:

SFOwner Share Owner

Where:

Share    is the share URL that you want to enumerate, like \\ServerName\ShareName.

Owner    is a mask that matches the owner, like:
         JSIINC\Jerry -  to match Jerry from JSIINC.COM exactly.
         \Jerry       -  to match both local and domain Jerry.
         Admin        -  to match Domain Admins, Administrator, and the Administrators group. 
         C\Je         -  to match Jerry and Jennifer from JSIINC.COM.
The output is displayed on the console, but can be redirected to a file. Each line of output contains:

"\\ServerName\ShareName\File System Object","Matching Sercurity Principal"

Like:

"\\JSI009\Zipnew","JSIINC\Jerry"
"\\JSI009\Zipnew\FAX","JSIINC\Jerry"
"\\JSI009\Zipnew\Saved\HappyHal.jpg","JSIINC\Jerry"
"\\JSI009\Zipnew\Saved\Harrods.pdf","JSIINC\Jennifer"
"\\JSI009\Zipnew\Saved\Voting_Machine.wmv","JSIINC\Jerry"
SFOwner.bat contains:
@echo off
if \{%2\}==\{\} @echo Syntax: call SFOwner Share Owner&goto :EOF
setlocal ENABLEDELAYEDEXPANSION
set object=%1
set object="%object:"=%"
set owner=%2
set owner=%owner:"=%
call WhoOwns %object% who
call set work=%%who:%Owner%=%%
if "%work%" NEQ "%who%" @echo %object%,"%who%"
for /f "Tokens=*" %%a in ('dir %object% /b /s /a') do (
 call WhoOwns "%%a" who
 call set work=!!who:%Owner%=!!
 if "!work!" NEQ "!who!" @echo "%%a","!who!"
)
endlocal


Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish