Try/Except for ADSI GetObject

L

LittlePython

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'
 
R

Roger Upole

LittlePython said:
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).

I don't get an error when doing a GetObject in VBS
for a nonexistent domain.


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.


You should be able to use win32net.NetValidateName to
verify that the domain exists before doing any more operations.

hth
Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top