Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document owner information 1

Status
Not open for further replies.

DougInCanada

Technical User
Feb 1, 2004
98
0
0
CA
does anyone know of WMI object.Item or API call to find the owner info of any documents/folders in a given folder using the 'Owner' attribute, much like the details view in Windows?
 
Hello DougInCanada,

I just post a proposal using adsi for your another thread.

For wmi approach, you can take a look win32_logicalfilesecuritysetting. Also you have to make use of win32_logicalfileowner associator with assoc-query. Check out the documentation.

regards - tsuji
 
Thanks for the pointer, Tsuji, but I loked at the win32_logicalfilesecuritysetting and didn't see any reference to the win32_logicalfileowner associator. The documentation link takes me to Adobe.com .could you put an example snippet together?
 
AHA!

I found this script in an excerpt from the book, Windows Management Instrumentation by Matthew Lavy. It works, but the file & path must be entered via command line parameter. Can anyone mod this script to run & read the file & path from a textfile?

'This script accompanies the book Windows Management Instrumentation
'by Matthew Lavy and Ashley Meggitt (New Riders, 2001)
'The code is copyright 2001, Matthew Lavy & Ashley Meggitt
'You are free to use and modify the script at will
Code:
'outputs the owner name of a file passed as a command-line
'parameter, or gives an error if it cannot
Option Explicit
On Error Resume Next
Dim refWMI
Dim strQuery
Dim colOwner
Dim refItem

'check command-line argument seems sane
If WScript.Arguments.Count <> 1 Then
	WScript.Echo"Usage: checkowner.vbs <path>"
	WScript.Quit
End If

'connect to WMI or quit with error
set refWMI = GetObject("winMgmts:")
If Err <> 0 Then
	WScript.Echo "Oh dear. Could not connect to WMI. Error: " & Err.Description
	WScript.Quit
End If

'get reference
strQuery = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=" & _
	"'" & WScript.arguments.item(0) & "'} WHERE " & _
	"AssocClass=Win32_LogicalFileOwner " & _
	"ResultRole=Owner"
set colOwner = refWMI.ExecQuery(strQuery)

'finally, read the owner info from Win32_SID
For Each refItem In colOwner
	If Err = 0 Then
		WScript.Echo WScript.arguments.item(0) & " is owned by " & refItem.AccountName
	Else
		WScript.Echo "Cannot find owner for " & WScript.arguments.item(0)
	End If
Next
Set colOwner = Nothing
set refWMI = Nothing
 
DougInCanada,

Now you get the difficult part done. I don't think anything can stop you doing the input part from reading a list of file from line-delimited text file. Look again for functionality of scripting.filesystemobject.

- tsuji
 
This is true, I can complete the part about reading paths/filenames from a text file and writing to another file the files found for a specific owner, for which I could use an input box prompt to aquire the username/owner I'm looking for.

My problem is this script is designed to run at the command line with an argument as a parameter (the path/filename). How do I convert this to a simple 'do until' loop instead of a command line where I have to provide an argument each time?
 
DougInCanada,

An argument is just a variable. Be it being passed from launching command line or being retrieved from within the script reading some file, the idea is the same.

[1] Suppose the file in question be "d:\test\abc.def", then make it in general be:
[tt] filespec="d:\test\abc.def"[/tt]

[2] Then feed the parameter to the corresponding instructions.
[tt] strQuery = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=" & _
"'" & [blue]filespec[/blue] & "'} WHERE " & _
"AssocClass=Win32_LogicalFileOwner " & _
"ResultRole=Owner"
[/tt] and
[tt] If Err = 0 Then
WScript.Echo [blue]filespec[/blue] & " is owned by " & refItem.AccountName
Else
WScript.Echo "Cannot find owner for " & [blue]filespec[/blue]
End If
[/tt]
[3] Now, the filespec can be retrieved say from some line-delimited data file. You use the usual operation to open it and read it line-by-line to get the filespec.
[tt] filespec=otextstream.readline[/tt]

[4] The whole thing is placed within a loop until you exhausted the file data (otextstream.endofstream=true)

[5] The .vbs is then launched without argument by doubleclick as usual.

- tsuji
 
Many Thanks, Tsuji!

I've only been playing with VBScripts for a little while, so thank you for providing some basics. I hope others find this as useful as I do.

 
I'm still having a problem getting the script to run. I've used a recursive search script to get all the file names with extensions and full UNC paths and list them in a text file. This file is C:\FileNames.txt.
But when I try to run the script below, I get an error on line 44, char 1 stating "error:80041002, source:(null)". I don't have enough experience with WMI to troubleshhot this generic message. Can anyone help me?

Code:
Option Explicit
'On Error Resume Next
Dim refWMI
Dim strQuery
Dim colOwner
Dim refItem
Dim FSO
Dim txtFileOwner
Dim txtFileName
Dim FileSpec

Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.FileExists ("C:\FileOwner.txt") then
    FSO.DeleteFile "C:\FileOwner.txt"
End If

Set txtFileOwner = FSO.CreateTextFile ("C:\FileOwner.txt")
txtFileOwner.close


Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set txtFileName = FSO.openTextFile("C:\FileNames.txt", ForReading)
Set txtFileOwner = FSO.openTextFile("C:\FileOwner.txt", ForAppending)


Do Until txtFileName.atendofstream=true

set refWMI = GetObject("winMgmts:")

filespec=txtFileName.readline

strQuery = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=" & _
    "'" & filespec & "'} WHERE " & _
    "AssocClass=Win32_LogicalFileOwner " & _
    "ResultRole=Owner"
set colOwner = refWMI.ExecQuery(strQuery)

For Each refItem In colOwner
    If Err = 0 Then
        txtFileOwner.writeline filespec & " is owned by " & refItem.AccountName
    End If
Next
Set colOwner = Nothing
set refWMI = Nothing

loop
 
DougInCanada,

If the file is on a remote m/c, then you have either [1] map the share to a drive or [2] bind refWMI to the remote m/c for the query. Try to map drive, then after the query remove it right away.

- tsuji
 
Hi, Tsuji.

The recursive search is being performed on a permenantly mapped drive which points to a file server share. The user has access permissions to all subfolders within this share.

Ideally, I am trying to create a script whereby a user can locate all the files for which they are the owner and therefore attributed to their disk quota.
 
DougInCanada,

But you mentioned unc,something like \\server\c$\datafile\abc.def; or just z:\datafile\abc.def? What I meant is if it is the former, you have to map z: to \\server\c$ or bind to the server in refWMI like:
getobject("winmgmts:\\"&server&"\root\cimv2")
Maybe you are saying that is configured so and still the error.

- tsuji
 
Hi again, Tsuji.

I have another, simpler code which I also tested and got the same result. The code is as follows:
Code:
Option Explicit
'On Error Resume Next

Dim FSO
Dim WSH
Dim txtFileOwner
Dim txtFileName
Dim FileSpec
Dim refSetting
Dim refSD
Dim refOwner

Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSH = CreateObject("WScript.Shell")

If FSO.FileExists ("C:\FileOwner.txt") then
    FSO.DeleteFile "C:\FileOwner.txt"
End If

Set txtFileOwner = FSO.CreateTextFile ("C:\FileOwner.txt")
txtFileOwner.close


Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set txtFileName = FSO.openTextFile("C:\FileNames.txt", ForReading)
Set txtFileOwner = FSO.openTextFile("C:\FileOwner.txt", ForAppending)


Do Until txtFileName.atendofstream=true


Set refSetting = GetObject("winMgmts:Win32_LogicalFileSecuritySetting='" & filespec & "'")

If refSetting.GetSecurityDescriptor(refSD) = 0 Then
	'operation was successful
	Set refOwner = refSD.Owner
	txtFileOwner.writeline refSetting.Path & " is owned by " & refOwner.Name
	Set refOwner = Nothing
	Set refSD = Nothing
End If
Set refSetting = Nothing

loop
As long as filespec is located on the local harddrive, the script works. If I try to use the UNC ("U:\foldername\subfoldername\filename") it fails. Thank you for explaining the binding syntax to the refWMI to the server. I think that might have been the problem. I'll test it right now and let you know.

Thanks again.
 
I must not be merging your information syntax properly.

This is the code I'm now testing:
Code:
Option Explicit
'On Error Resume Next

Dim FSO
Dim WSH
Dim txtFileOwner
Dim txtFileName
Dim FileSpec
Dim refSetting
Dim refSD
Dim refOwner
DIM SERVER

Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSH = CreateObject("WScript.Shell")

If FSO.FileExists ("C:\FileOwner.txt") then
    FSO.DeleteFile "C:\FileOwner.txt"
End If

Set txtFileOwner = FSO.CreateTextFile ("C:\FileOwner.txt")
txtFileOwner.close


Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set txtFileName = FSO.openTextFile("C:\FileNames.txt", ForReading)
Set txtFileOwner = FSO.openTextFile("C:\FileOwner.txt", ForAppending)


Do Until txtFileName.atendofstream=true

Set SERVER = FileServ1

Set refSetting = getobject("winmgmts:\\"& SERVER &"\root\cimv2 Win32_LogicalFileSecuritySetting='" & filespec & "'")

If refSetting.GetSecurityDescriptor(refSD) = 0 Then
	'operation was successful
	Set refOwner = refSD.Owner
	txtFileOwner.writeline refSetting.Path & " is owned by " & refOwner.Name
	Set refOwner = Nothing
	Set refSD = Nothing
End If
Set refSetting = Nothing

loop
Now it's giving the error "Variable undefined: FileServ1"

help???
 
DougInCanada,

Just a rough look at your code. The loop, should it not start with:
[tt] Do Until txtFileName.atendofstream[/tt]

I've to go off-line, maybe later.

- tsuji
 
If FileServ1 is the name of your server, then put quotes "" around the name. As it is, it is taken as a variable.

Tsuji was correct. Start the loop with:
Code:
Do While Not txtFileName.atendofstream=true

Spong
 
Tsuji,

I've found that it works either way, just a habit from other coding practices. Either way, it still generates the same error as above...I can't seem to get the server name to be defined.
 
DougInCanada,

You're right, it's just a habit. The rest, I'll think about it.

- tsuji
 
DougInCanada,

Indeed, what is FileServ1?

- tsuji
 
DougInCanada,

[1] I see what you meant. It should not use set keyword for server (name) read out from the textfile. The corresponding line can be written as:
[tt] SERVER = "FileServ1"[/tt]

[2] In your latest script, your filespec is not read! Your approach seems changed to using getsecuritydescriptor...
using a different approach than you're previously using? If I keep the same original approach, the code with your intended file output would roughly look like.
Code:
Option Explicit
'On Error Resume Next

Dim FSO
'Dim WSHSHELL
Dim txtFileOwner
Dim txtFileName
Dim FileSpec
'Dim refSetting
'Dim refSD
'Dim refOwner
Dim SERVER
Dim refWMI
Dim strQuery
Dim refItem
Dim colOwner

Set FSO = CreateObject("Scripting.FileSystemObject")
'Set WSHSHELL = CreateObject("WScript.Shell")

If FSO.FileExists ("C:\FileOwner.txt") then
    FSO.DeleteFile "C:\FileOwner.txt"
End If

Set txtFileOwner = FSO.CreateTextFile ("C:\FileOwner.txt", true)
txtFileOwner.close

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set txtFileName = FSO.openTextFile("C:\FileNames.txt", ForReading)
Set txtFileOwner = FSO.openTextFile("C:\FileOwner.txt", ForAppending)

SERVER = "FileServ1"

Do Until txtFileName.atEndOfStream=true

    FileSpec = txtFileName.readline

    Set refWMI = GetObject("winmgmts:\\"& SERVER &"\root\cimv2")
    strQuery = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=" & _
        "'" & filespec & "'} WHERE " & _
        "AssocClass=Win32_LogicalFileOwner " & _
        "ResultRole=Owner"

    Set colOwner = refWMI.ExecQuery(strQuery)

    On Error Resume Next
    For Each refItem In colOwner
        If Err = 0 Then
            'WScript.Echo FileSpec & " is owned by " & refItem.AccountName
            txtFileOwner.writeline FileSpec & " is owned by " & refItem.AccountName
            Err.clear
        Else
            'WScript.Echo "Cannot find owner for " & FileSpec
            txtFileOwner.writeline "Cannot find owner for " & FileSpec
        End If
    Next
loop

txtFileOwner.close
txtFileName.close
Set txtFileOwner = Nothing
Set txtFileName = Nothing
Set ColOwner = Nothing
Set refWMI = Nothing
(I have a terrible time cut and paste---so much confusion! So cannot gurrantee.) Besides, why you changed approach? There are many trouble spots in you latest script. Simply cannot rely on it getting results without many revision!

[3] You use wsh for wscript.shell. I advise you not to. Name it whatever else, wsh is reserved. It will keep you out of trouble only if you dim wsh it specifically.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top