0

I'm trying to pass variable to create account in active directory, but i'm not sure how to pass it. It is creating an account in the variable name, not the value of variable.

import ldap3
conn = ldap3.Connection("172.16.251.61", "SWINTERNAL\\ad.manager", "Adm@n@ger@1", auto_bind=True)
commonname = "honda"
conn.add('cn= commonname,ou=POC,dc=SWINTERNAL,dc=LOCAL', 'User', {'givenName': 'Beatrix', 'sn': 'Young', 'departmentNumber': 'DEV', 'telephoneNumber': 1111})
  • *"creating an account in the variable name"* - Can you explain more about what you mean by that? – Gabriel Luci Dec 24 '21 at 00:43
  • This is a code which creates an active directory account using python. I have created a variable name "commonname" and assigned the value "honda". I want to pass the variable into "conn.add('cn= commonname,ou=POC,,dc=LOCAL', 'User', {'givenName': 'Beatrix')" . It is creating an account with the variable name, not with the value that is in the variable. – gokul kumar Dec 24 '21 at 05:01

1 Answers1

0

I think you're looking for Python's string interpolation, which involves prefixing the string with f and enclosing the variable name in { }. Like this:

conn.add(f'cn={commonname},ou=POC,dc=SWINTERNAL,dc=LOCAL', 'User', {'givenName': 'Beatrix', 'sn': 'Young', 'departmentNumber': 'DEV', 'telephoneNumber': 1111})

That will replace {commonname} with the value of the commonname variable.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84