Solved

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

Posted on 2014-01-09
9
1,885 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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
 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Free software 16 58
troubleshoot a python script 8 113
Python sort list of tuples, skipping column 2 61
78 files, need to delete row 2 in every file 3 30
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

737 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