Try/Except for ADSI GetObject

LittlePython LittlePython at lost.com
Wed May 31 02:22:30 CEST 2006


I am a little confused on why I can not detect an object that does not exist
with a try and except. If I understand ADSI correctly from what I have read
you do not create these objects but rather get them. They already exist. I
believe if I do the equivalent in VB I would generate an error when I try to
get an object that does not exist (can not find).  What have I done wrong?
I have included the function below that works in ever respect but detecting
incorrect NT Domain Names (objects that do not exist) . Any tips, insight or
comments would be welcome by this newbie.

Thx


def CheckNT4(header, all):

    adsi = win32com.client.Dispatch('ADsNameSpaces')

    try:
        nt = adsi.GetObject('', "WinNT://"+ frm.NTName) #----->I need some
type of try/except to detect incorrect NT Domain Names
    except: print "Domain name failed" # this does not detect a problem

    nt.Filter = ['user']
    try:
        oSID = win32com.client.Dispatch('ADsSid')
    except:
        MyUsers = [  eauser for eauser in all if eauser[GetPostion(header,
'STATUS')] is  '']
        for ea in MyUsers:
            ea[GetPostion(header, 'STATUS')] = 'FailNT'
            ea[GetPostion(header, 'COMMENT')] ='Can not find ADsSid'
    MyUsers = [  eauser for eauser in all if eauser[GetPostion(header,
'STATUS')] is  '']
    intcnt = 0
    for user in nt:
        for ea in MyUsers:
            if string.upper(user.Name) == string.upper(ea[GetPostion(header,
'OLDNTLOGON')]):
                frm.MyProgress.Update ( intcnt, 'Found NT user and SID : ' +
user.Name )
                intcnt = intcnt+ 1

                if user.AccountDisabled:
                    ea[GetPostion(header, 'STATUS')] = 'FailNT'
                    ea[GetPostion(header, 'COMMENT')] ='Disabled'
                else:
                    if ea[GetPostion(header, 'COMMENT')] is not '':
ea[GetPostion(header, 'COMMENT')] = ''
                    oSID.SetAs (5, "WinNT://"+ frm.NTName + '/' + user.Name)
                    ea[GetPostion(header, 'STATUS')] = 'PassNT'
                    ea[GetPostion(header, 'Nt4Sid')] = oSID.GetAs(1)

    for user in [ eauser for eauser in all if eauser[GetPostion(header,
'STATUS')] is  '']:
        user[GetPostion(header, 'STATUS')] = 'FailNT'
        user[GetPostion(header, 'COMMENT')] = 'NoNt4Account'






More information about the Python-list mailing list