How Can I Determine the Owner of a File?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there a way to determine the owner of a file by using a script?

— BD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, BD. As a matter of fact there is a way to use a script to determine the owner of the file, though it’s understandable why you might not be able to find that information on your own. After all, there are two main scripting interfaces used for managing files: the Script Runtime’s FileSystemObject and the WMI class CIM_DataFile. Seeing as how neither of these interfaces includes a property or method for determining file ownership, the logical conclusion would be, “Oh, I guess you can’t do this after all.”

But you know how it is with scripting: things can often be done, though not the way you might logically expect to do them. In this case, you need to use WMI’s Win32_LogicalFileSecuritySetting class in conjunction with the Win32_LogicalFileOwner association class to determine file ownership. As you might expect, the LogicalFileSecuritySetting class grabs security information from a file. What it can’t do, however, is tell you the name of the file owner. That’s where the Win32_LogicalFileOwner class comes into play: it takes the owner’s SID (security identifier) and relays it to the Win32_SID class. The Win32_SID class can then lookup and report the owner name and domain.

Confused? We don’t blame you; association classes are not the most intuitive thing ever created. Fortunately, however, you don’t need to understand how association classes work; just rest assured that they do work. For example, here’s a script that reports the owner of the file C:\Scripts\My_script.vbs:

On Error Resume Next

strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _ & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)

strFile = “C:\Scripts\My_script.vbs”

Set colItems = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='” & strFile & “‘}” _ & ” WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner”)

For Each objItem in colItems Wscript.Echo objItem.ReferencedDomainName Wscript.Echo objItem.AccountName Next

Looks crazy, but it will do the job. And what if you need to get the owner of a different file? No problem: just set the value of the variable strFile to the complete path of that file.

And don’t let the names mislead you: these two classes can also be used to determine the owner of a folder. For example, this script reports back the owner of the folder C:\Scripts:

On Error Resume Next

strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _ & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)

strFile = “C:\Scripts”

Set colItems = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='” & strFile & “‘}” _ & ” WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner”)

For Each objItem in colItems Wscript.Echo objItem.ReferencedDomainName Wscript.Echo objItem.AccountName Next

0 comments

Discussion is closed.

Feedback usabilla icon