views:

95

answers:

3

At work, we have short login names, e.g. hastingsg, but Outlook and I believe other parts of the Windows system also have access to a longer name, e.g. Jeff Hastings.

In cpython (not IronPython), if I have the shorter login name, how can I get the longer full name? I have pywin32 and ExchangeCDO installed.

+1  A: 

I think you can query your Exchange Server Active Directory with a dedicated module

(not tested):

import active_directory
user = active_directory.find_user("hastingsg")
print user.displayName
RC
That gave me a result of 'hastingsg' again, but when I looked at the other properties, user.geco gave me something like what I want.Thanks!
Ross Rogers
fyi... user.geco could be a corporate setting which is unique to my setup.
Ross Rogers
ooops. it was user.gecos not user.geco
Ross Rogers
+1  A: 

Via the COM parts of pywin32, you need to get Outlook's Application object, and from it its attribute Session, which gives you the Namespace object (the GetNamespace method should also work for the same purpose, when called with the only supported argument value, 'MAPI'). From there you can use the Accounts property to get the Accounts object, which is a typical COM collection -- indexable via Item up to its Count. You loop over it and check each Account object: each has two properties of interest -- a UserName (the string you want to check for equality to the "shorter login name") and a DisplayName -- the string you desire.

Yes, this is incredibly long and convoluted, but, that's par for the course for the COM interfaces that MS applications offer. For all I know there might be leaner way in recent Outlook releases -- this is the long and gnarled way that's been working for a long time (these days I don't even have a Windows install handy to check this out and write the Python for you...!-)

Alex Martelli
A: 

May be you need TranslateName api e.g. play with something like this, 2ns /3rd argument can be constants from http://msdn.microsoft.com/en-us/library/ms724268%28VS.85%29.aspx

win32security.TranslateName(win32api.GetUserName(), win32api.NameUnknown, win32api.NameDisplay)

or

win32net.NetUseGetInfo(win32api.GetComputerName(),win32api.GetUserName())
Anurag Uniyal
The first code returned the original user name and the second errored with: pywintypes.error: (2250, 'NetUseGetInfo', 'The network connection could not be found.')
Ross Rogers
in first you can try different 3rd argument e.g. see http://msdn.microsoft.com/en-us/library/ms724268(VS.85).aspx
Anurag Uniyal