Aaron T Granberg
(Fresh Scripter)
2002-06-20 09:13 PM
script that lists the owner of files

I was wondering if it is possible to write a KIX script to look at all the files in a directory (WinNT 4.0 TSE SP6) and list who the owners of the files are. This particular folder has over 12000 files in it and I am hoping that KIX could help me to start organizing it. If anyone has any insight into how I could do this with KIX or something else I would GREATLY appreciate it.

Thanks.


BrianTX
(Korg Regular)
2002-06-20 10:28 PM
Re: script that lists the owner of files

There is a way.. give me a few and I'll whip up a script (if someone doesn't beat me to it).

Brian


BrianTX
(Korg Regular)
2002-06-20 10:31 PM
Re: script that lists the owner of files

I can do it through COM... but do you need to know the owner? Or do you need to know who has rights to change the file?

Brian


Aaron T Granberg
(Fresh Scripter)
2002-06-20 11:24 PM
Re: script that lists the owner of files

I'd like to know who the owner is so I can tell people what files belong to them so they can clean them up.

I really appreciate your help in this.


BrianTX
(Korg Regular)
2002-06-20 11:52 PM
Re: script that lists the owner of files

It's not as straightforward as I thought, but I'm making progress... [Smile]

Brian


BrianTX
(Korg Regular)
2002-06-21 02:12 PM
Re: script that lists the owner of files

I've got something that will check the owner of a file.. now i'm reworking it! [Smile]

Brian


BrianTX
(Korg Regular)
2002-06-21 03:55 PM
Re: script that lists the owner of files

Okay.. here is my code.. it probably needs to be cleaned up a bit.. and maybe reworked for your needs.. but what it does is create a file that lists the filename with the ownership..

code:
break on

Function GetOwner($fileinput)
$wmiobj = GetObject("Winmgmts:")
$SIDobj = $wmiobj.ExecQuery("ASSOCIATORS OF {win32_LogicalFileSecuritySetting='$fileinput'}
WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
For each $SID in $SIDobj
$GetOwner=$SID.ReferencedDomainName+"\"+$SID.AccountName
Next
EndFunction


$fileOutput = "i:\owners.txt" ; place to store the owner list
$dir = "m:\" ; directory path. UNC names not supported. Must end in "\"

$ok = RedirectOutput($fileoutput)
$filename = DIR($dir)
While $filename <> "" AND @error = 0
$file = $dir+$filename
$owner = GetOwner($file)
? $file+" "+$owner
$filename = DIR()
Loop
$ok = RedirectOutput("")

If you need it cleaned up to perform tasks based on owner and you need help with that, let me know.

Brian

[ 21 June 2002, 17:58: Message edited by: BrianTX ]


BrianTX
(Korg Regular)
2002-06-21 05:19 PM
Re: script that lists the owner of files

If anyone else that is good at returning arrays in functions wants to help me create a UDF that returns

GetOwner[0] as username
&
GetOwner[1] as domain

I'd appreciate the help. I don't know why, but I was fiddling with it earlier and having a lot of problems.

Brian


ShawnAdministrator
(KiX Supporter)
2002-06-21 05:45 PM
Re: script that lists the owner of files

Would this be where your driving with this, Brian ?

break on

Function GetOwner($fileinput)
 dim $a[1]
 $GetOwner=0
 $wmiobj = GetObject("Winmgmts:")
 $SIDobj = $wmiobj.ExecQuery("ASSOCIATORS OF {win32_LogicalFileSecuritySetting='$fileinput'}
                               WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
  For each $SID in $SIDobj
   $a[0] = $SID.AccountName
   $a[1] = $SID.ReferencedDomainName
   $GetOwner=$a
  Next
EndFunction

$file = "c:\temp"
$array = GetOwner($file)
?"AccountName=" $array[0]
?"DomainName=" $array[1]



-Shawn

{EDIT}

Guess you could do this as well:

Function GetOwner($fileinput)
 redim $GetOwner[1]

 ...

 For each $SID in $SIDobj
  $GetOwner[0] = $SID.AccountName
  $GetOwner[1] = $SID.ReferencedDomainName
 Next



--Shawn

[ 21 June 2002, 17:48: Message edited by: Shawn ]


BrianTX
(Korg Regular)
2002-06-21 05:57 PM
Re: script that lists the owner of files

That was kind of what I had in mind.. but what if someone gives an invalid file or folder? Does it still return an array? Or does it break? When I tried it, it broke for some reason.

Brian


Howard Bullock
(KiX Supporter)
2002-06-21 05:57 PM
Re: script that lists the owner of files

See TranslateNAme() also. I use
code:
Function TranslateNAme()
...
$TranslateName = $ReturnName, $Error, $ErrorText
Endfunction

there and it works fine.

So

$GetOwner = $SID.AccountName, $SID.ReferencedDomainName

[ 21 June 2002, 17:58: Message edited by: Howard Bullock ]


BrianTX
(Korg Regular)
2002-06-21 06:07 PM
Re: script that lists the owner of files

I tried all but the first method, guys, and had a problem with the script... (strange).. I'm notoriously bad about including error-correction in my scripts, so anyone want to volunteer to add such to the UDF and post it under the UDF forum? (I know I should do it, myself, but the fun part of figuring out how it works is done, and I don't want to wash the dishes and put them away! lol)

Brian


ShawnAdministrator
(KiX Supporter)
2002-06-21 06:08 PM
Re: script that lists the owner of files

I would rig it so that the funtion set @ERROR, and if it was successfull, the user can assume the function returned an array, and if it not, just have the function return a 0 (zero).

ShawnAdministrator
(KiX Supporter)
2002-06-21 06:15 PM
Re: script that lists the owner of files

You might wonder why you would want to return a 0 (zero). Because that would stop a user that used you UDF in a for-each-in-next statement, from actually running the loop. If you return a 0, the for loop never executes, and the script just carries on - ie, no data !

BrianTX
(Korg Regular)
2002-06-21 06:23 PM
Re: script that lists the owner of files

So, do you mean assign 0 to each element in the array? Or just return 0? I would think to be consistent, a function should always return the same type with the same number of elements unless the user can specify a difference when calling the function.

Brian


ShawnAdministrator
(KiX Supporter)
2002-06-21 06:27 PM
Re: script that lists the owner of files

Well, this could proably turn into quite a debate, but I figured you could (if you wanted), only return a valid array, or nothing (zero). In this case, the user probably would NOT iterate over your array using for-each-in-next anyways. Its up to you Brian ... the most important thjing is to do whatever makes sense to you !

[ 21 June 2002, 18:28: Message edited by: Shawn ]


BrianTX
(Korg Regular)
2002-06-24 03:03 PM
Re: script that lists the owner of files

Aaron.. is this what you were looking for? We'd love to find out how things turned out for you.

Brian


Mark Bennett
(Getting the hang of it)
2002-06-24 03:22 PM
Re: script that lists the owner of files

The Win2k (and NT4) ResKits have some Perl scripts that will do this for you. I have used one in the past to establish owners then 'pruned' the output to a format usable by my script.

Aaron T Granberg
(Fresh Scripter)
2002-06-24 08:50 PM
Re: script that lists the owner of files

Thanks a lot you guys. I am totally impressed. I am doing so tests with it and I will let you guys know how it goes.