Bulk Modify AD User Properties Python script
The main is to use Python call dsquery query to the DN, and then use Python call dsmod to make the DN property modification.
Namelist.txt format as follows, the Department of the field can actually be changed to the corresponding ad attributes, such as the title of the ad in the position, want to modify the job number-position, the dsmod statement is changed to the following (specific dsmod user parameters dsmod user/? can see. )
"dsmod user" + DN + "-title" + dep
|---------------|
|SAMACCOUNT,DEP |
| work number, Department |
|---------------|
The code below requires administrator privileges to execute dsmod:
#coding =utf-8import osdef getdn (Gonghao): z = os.popen ("dsquery user -samid + gonghao). Read () return z def cud (DN, DEP): ' change users depatment ' try: z = os.popen ("dsmod user " + dn + " -dept " + DEP". Read () return z except:print ' Can ' t find user ' dicfile = open (' namelist.txt ', ' R ') dic1 = {}dic2 = {} #make dic1,dic1 format from text filefor line in dicfile: a = line.strip (). Split (', ') dic1[a[0]] = a[1]dicfile.close () #print ' Dic1 ', Dic1#make dic2, dic2 is {dn:depatement}foR k,v in dic1.iteritems (): print k,v dic2[ Getdn (k)] = vprint ' * ' *10,dic2for i in dic2:# print ' i=%s ' %i, ' dic2[i]=%s ' %dic2[i] if dn== ':p ass Else:cud (I.strip (), Dic2[i])
This article from the "July" blog, reproduced please contact the author!
Bulk Modify AD User Properties Python script