2017-03-31 3 views
-1

するtry/except巻き付け実行していない:が正しく、私はこのようになります機能を持っている

それは、ユーザ情報とYAMLファイルで呼び出す
def get_users(yaml_file="AD_Users.yml"): 
with open(yaml_file, 'r') as stream: 
    try: 
     data = yaml.load(stream) 
    except yaml.YAMLError as exc: 
     print(exc) 
return data.itervalues() 

def create_user_in_ad(new_users): #username, password, base_dn, fname, lname, domain): 
    # LDAP connection 
    for new_user in new_users: 
     try: 
      logging.info('Connecting to LDAP Server %s ' % LDAP_SERVER) 
      ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, 0) 
      ldap_connection = ldap.initialize(LDAP_SERVER) 
      ldap_connection.simple_bind_s(BIND_DN, BIND_PASS) 
      print "Connected to LDAP Server!" 
     except ldap.LDAPError, error_message: 
      print "Error connecting to LDAP server: %s" % error_message 
      #logging.info('Unable to connect to LDAP Server %s ' % LDAP_SERVER) 
      return False 
#   sys.exit(1) 

     # Check and see if user exists 
     try: 
      print new_user['username'] 
      logging.info('Querying AD for user: %s ' % new_user['username']) 
      user_results = ldap_connection.search_s(BASE_DN, ldap.SCOPE_SUBTREE, 
                '(&(sAMAccountName=' + 
                new_user['username'] + 
                ')(objectClass=person))', 
                ['distinguishedName']) 
      print user_results 
     except ldap.LDAPError, error_message: 
      print "Error finding username: %s" % error_message 
      logging.info('Unable to query for user: %s ' % new_user['username']) 
      return False 

     # Check the results 
     if len(user_results) != 0: 
      print "User", new_user['username'], "already exists in AD:" 
      return False 
      sys.exit(1) 

     # Lets build our user: Disabled to start (514) 
     USER_DN = 'cn=' + new_user['fname'] + ' ' + new_user['lname'] + ',' + BASE_DN 
     GROUP_DN = 'ou=' + new_user['group_dn'] + ',' + BASE_DN 
     user_attrs = {} 
     user_attrs['objectClass'] = \ 
        ['top', 'person', 'organizationalPerson', 'user'] 
     user_attrs['cn'] = new_user['fname'] + ' ' + new_user['lname'] 
     user_attrs['userPrincipalName'] = new_user['username'] + '@' + new_user['domain'] 

     # Add the new user account 
     try: 
      ldap_connection.add_s(USER_DN, user_ldif) 
      logging.info('Adding user into AD: %s ' % new_user['username']) 
     except ldap.LDAPError, error_message: 
      print "Error adding new user: %s" % error_message 
      return False 

     # Add the password 
     try: 
      ldap_connection.modify_s(USER_DN, add_pass) 
      logging.info('Password added for user: %s ' % new_user['username']) 
     except ldap.LDAPError, error_message: 
      print "Error setting password: %s" % error_message 
      return False 

     # Change the account back to enabled 
     try: 
      ldap_connection.modify_s(USER_DN, mod_acct) 
      logging.info('Enabling AD Account for user: %s ' % new_user['username']) 
     except ldap.LDAPError, error_message: 
      print "Error enabling user: %s" % error_message 
      return False 

     # Add user to their primary group 
     try: 
      ldap_connection.modify_s(GROUP_DN, add_member) 
      logging.info('Adding user to group %s: ' % new_user['group_dn']) 
     except ldap.LDAPError, error_message: 
      print "Error adding user to group: %s" % error_message 
     return 
new_users = get_users() 
if args.createusers is not "None": 
    create_user_in_ad(new_users) 

:今

User1: 
    username: adtest2 
    fname: adtest2 
    lname: adtest2 
    domain: test.com 
    group_dn: test 
# group: 

    # User2: 
    # username: testing 
    # fname: testing 
    # lname: bbbbb 
    # domain: test.com 
    # group_dn: test 
# # group: 

    ldap_connection.unbind_s() 

    print "User %s has been created in AD!" % new_user['username'] 

    # All is good 
    return True 

問題コメントアウトされていないときに2番目のユーザーのみが実行されています。私はforループで何か問題があったのでしょうか?それは私がそれが適切にやりたいと思っているすべてをしています。私はPython 2.7.13 btwを使用しています!

出力は今だけ第二のユーザを示しています

Connected to LDAP Server! 
adtest2 
+0

2番目のユーザーのみで実行されていると思われる出力は何ですか。 'print new_user'を追加した場合。 – Roelant

+0

私は、あなたがnew_userのユーザ名を印刷しているのを見ることができるクエリの結果を印刷しています。私もprint new_user ['username']を追加してユーザーのユーザー名をコードに出力し、2番目の人だけを返しました – user2019182

+0

質問を出力で更新しました – user2019182

答えて

0

問題はリターンがためのループの内部で実行されていたということでした。私は単にforループの外に移動しました。

関連する問題