Windows Groups and Python

posted @ Monday, October 18, 2004 1:34 AM


So because of my recent interests with IronPython, I decided I should learn Python to better understand everything. Python really appears to be a nice language (from the weekend that I spent with it). It feels slightly strange not using curly braces to scope methods though. Anyway, I wanted to come up with a nice example of how Python works within Windows to help me better understand the language. I was watching Crossing Jordan tonight with Megan when I though of the script I threw together on Friday at work and how it would be a great sample to test Python. After I downloaded the Python Win32 extensions, I was off and running. After a little fiddling, this is what I came up with, which is very close to my original script:


import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
strName = shell.ExpandEnvironmentStrings("%USERNAME%")
strDomain = shell.ExpandEnvironmentStrings("%USERDOMAIN%")
objUser = win32com.client.GetObject("WinNT://" + strDomain + "/" + strName)
strMsg = strName + " belongs to the following groups:\n\n"
for group in objUser.Groups():
strMsg += group.Name + "\n"