Unicode Error

Tim Golden tim.golden at viacom-outdoor.co.uk
Wed Aug 23 04:25:05 EDT 2006


[Gallagher, Tim (NE)]

| Hey all I am learning Python and having a fun time doing so.  
| I have a question for y'all, it has to do with active directory.

| I want to get the last login for a computer from Active 
| Directory.  I am using the active_directory module and here 
| is my code.

[START]
import active_directory
computer = active_directory.root()
for cpu in computer.search ("cn='Computer_Name'"): 
    print cpu.samAccountName                    #←--- Works find
    print cpu.operatingSystem                   #←--- Works find
    print cpu.lastLogon                         #←--- Getting Error
[END]

| I get an error that I am not sure what to do with, the error 
| is TypeError: coercing to Unicode: need string or buffer, 
| instance found in my line Do I have to change the output to 
| meet Unicode formation?

I started to write an explanation of Unicode and what an
encoding was and why you needed it, but then I realised
that it wouldn't help - at least not here - because the
problem seems to involve converting the value in cpu.lastLogon
to Unicode. And I'm not sure why it's even trying to do that.

The lastLogon value (according to the MS docs) is actually
a structure in its own right with a HighPart and a LowPart,
and you perform various maths on these numbers to give
you a real date. In my case (cf code below) if I simply print the 
lastLogon, I get the anonymous <COMObject> string.

<code>
import active_directory
me = active_directory.find_computer ()
print me.samAccountName

print me.lastLogon 
# gives <COMObject <unknown>>

print me.lastLogon.HighPart, me.lastLogon.LowPart
# gives two long numbers

</code>

Short answer, try lastLogon.HighPart & lastLogon.LowPart

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list