Solved

Can I use Python to retrieve a file owner in a Windows environment?

Posted on 2014-01-09
9
1,584 Views
Last Modified: 2014-01-14
Using Python, I would like to iterate through a series of files, obtaining the owner name (not the UID) from each of them.  In the Unix environment, the pwd module allows you to do this.  Is there anything comparable in the Windows environment?
0
Comment
Question by:cshore12
  • 4
  • 3
  • 2
9 Comments
 
LVL 19

Assisted Solution

by:Thommy
Thommy earned 250 total points
ID: 39770295
In windows you can use PowerShell to determine owner of a file or multiple files...



PowerShell-Command for a specific file
Get-Acl C:\Scripts\Test.txt

Result:
Path                          Owner                         Access
----                          -----                         ------
Test.txt                      FABRIKAM\kenmyer              BUILTIN\Administrators Allow  FullCo...

PowerShell-Command for multiple files
Get-Acl C:\Scripts\*.*

Result:
Path                          Owner                         Access
----                          -----                         ------
Example.txt          FABRIKAM\kenmyer              BUILTIN\Administrators Allow  FullCo...
Test.txt                  FABRIKAM\pilarackerman    BUILTIN\Administrators Allow  FullCo...
Trial.txt                 FABRIKAM\kenmyer              BUILTIN\Administrators Allow  FullCo...
0
 

Author Comment

by:cshore12
ID: 39771017
Yes, that will do it -- thank you!

I'll be writing a larger script that uses some standard logic and utilities, seach as iterating through folders, searching, matching strings, etc.  So far I've only read a small part of the tutorial; can I assume that PS is capable of such functionality?
0
 
LVL 19

Expert Comment

by:Thommy
ID: 39771069
Powershell as its name already says is very powerful.

You can loop through directory structures...
ForEach Loop through a directory

String operations are also no problem...
The String’s the Thing
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 250 total points
ID: 39774246
If you want to use python, you can use the functions (GetFileSecurity and LookupAccountSid) from the win32security library (included with pywin32) to retrieve the owner information:

import win32security

def GetOwner(filename):
    f = win32security.GetFileSecurity(filename, win32security.OWNER_SECURITY_INFORMATION)
    (username, domain, sid_name_use) =  win32security.LookupAccountSid(None, f.GetSecurityDescriptorOwner())
    return username

print GetOwner(r"c:\somefile.txt")

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:cshore12
ID: 39774970
clockwatcher-

I tried your code, but was told that the win32security module doesn't support the GetFileSecurity method.  I'm running Py 2.7.2, and also downloaded the pywin32 executable.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 39775321
Can you post your exact error message?  

The win32security module definitely supports the GetFileSecurity method:

   http://docs.activestate.com/activepython/2.7/pywin32/win32security__GetFileSecurity_meth.html

I've tested my code on Windows 7 and 8 and works fine under various python versions (including 2.7).  What OS are you on?
0
 

Author Comment

by:cshore12
ID: 39776874
clockwatcher:

I'm using Win 7 Enterprise, sp1.  Here's the traceback:

Traceback (most recent call last):
  File "C:\Other\win32security.py", line 1, in <module>
    import win32security
  File "C:\Other\win32security.py", line 8, in <module>
    print GetOwner(r"c:\Other\CHICKEN_STUFF.doc")
  File "C:\Other\win32security.py", line 4, in GetOwner
    f = win32security.GetFileSecurity(filename, win32security.OWNER_SECURITY_INFORMATION)
AttributeError: 'module' object has no attribute 'GetFileSecurity'
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 39778342
It appears you named your program win32security.py.  You can't do that and then load a module with the same name.  If you name your program, win32security.py and then do a:

  import win32security

From within that same program, you're loading your own program (not the win32security module that is installed with pywin32) as a module.

Long story short, rename your file something other than win32security.py so it doesn't conflict with and override the standard module.
0
 

Author Comment

by:cshore12
ID: 39779262
Yes, that was stupid of me!  I renamed the file, and the win32security module works great now.

I'm going to award each of you an equal share.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 81
C# Error - Add Failed 12 78
Need help with python script - Insert data from user input into MS SQL 35 67
CPU at 100% usage, why? 27 80
If your app took Google’s lash recently, here are the 5 most likely reasons.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to set up a document for the web and print and the recommended PPI for printing.
This video shows how use content aware, what it’s used for, and when to use it over other tools.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now