Try/Except for ADSI GetObject

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LittlePython

    Try/Except for ADSI GetObject

    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('ADsN ameSpaces')

    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('ADsS id')
    except:
    MyUsers = [ eauser for eauser in all if eauser[GetPostion(head er,
    'STATUS')] is '']
    for ea in MyUsers:
    ea[GetPostion(head er, 'STATUS')] = 'FailNT'
    ea[GetPostion(head er, 'COMMENT')] ='Can not find ADsSid'
    MyUsers = [ eauser for eauser in all if eauser[GetPostion(head er,
    'STATUS')] is '']
    intcnt = 0
    for user in nt:
    for ea in MyUsers:
    if string.upper(us er.Name) == string.upper(ea[GetPostion(head er,
    'OLDNTLOGON')]):
    frm.MyProgress. Update ( intcnt, 'Found NT user and SID : ' +
    user.Name )
    intcnt = intcnt+ 1

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

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



  • Roger Upole

    #2
    Re: Try/Except for ADSI GetObject


    "LittlePyth on" <LittlePython@l ost.com> wrote in message news:al5fg.9877 $ho6.1459@trndd c07...[color=blue]
    >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).[/color]

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


    What have I done wrong?[color=blue]
    > 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.[/color]


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

    hth
    Roger




    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---

    Comment

    Working...