Mark Hammond wrote:
>> it's possible, but not slick. If you wanted to follow their
>> line and use WMI to access the registry, you could additionally
>> use the WMI Win32_UserAccount class to work out the SID you need.
>> For example, to find my profile on this machine, the following
>> seems to work:
>>
>> (uses the wmi module from http://timgolden.me.uk/python/wmi.html)
>>
>> <code>
>> import _winreg
>> import win32api
>> import wmi
>>
>> #
>> # Use the current username in DOM\USER format
>> #
>> USERNAME = win32api.GetUserNameEx (2)
>> ## USERNAME = "GOYLE\\tim"
>>
>> HKLM = 0x80000002
>> profiles_key = r"SOFTWARE\Microsoft\Windows
>> NT\CurrentVersion\ProfileList"
>>
>> c = wmi.WMI (find_classes=False)
>> for account in c.Win32_UserAccount (Caption=USERNAME):
>>    sid = account.SID
>>    break
>> else:
>>    raise Exception, "User %s not found" % USERNAME
> 
> FYI, note that getting a SID is as simple as:
> 
>>>> import win32security
>>>> user_sid = win32security.LookupAccountName(None, "Administrator")[0]
>>>> str(user_sid)
> 'PySID:S-1-5-21-...'

I knew that (and referred to it in the parallel thread
on c.l.py). The only reason I offered a WMI solution here
was because the article I linked to above was a WMI one
-- to do something slightly different -- and my starting
point was more or less of a straight translation.

Also, perhaps, I find that (reducing my code a little to
match yours):

   import wmi
   c = wmi.WMI (find_classes=0)
   account = c.Win32_UserAccount (Name="Administrator")[0].SID

isn't so far removed in simplicity from:

   import win32security
   user_sid = win32security.ConvertSidToStringSid (
     win32security.LookupAccountName(None, "Administrator")[0]
   )

which, as far as I can see, is its more exact equivalent, since
you need "S-1-5-21..." to match against the registry in the next
step. (Which you can do with the win32api or _winreg registry
functions, too).

Funnily enough, when I set out to answer the OP's question,
I had no intention of producing a WMI answer; it's only
because I happened upon that technet article that I went
that way!

TJG
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to