批量修改AD用户属性python脚本
主要是用python调用dsquery查询到dn,再用python调用dsmod进行dn的属性修改。
namelist.txt格式如下,部门的字段其实可以换成相应想修改的AD属性,比如AD中title是表示职位,想要修改工号-职务,就将dsmod语句改成如下(具体dsmod user的参数查 dsmod user /?可以看到。)
"dsmod user " + dn + " -title " + dep
|---------------||sAMAccount,dep |
|工号,部门 |
|---------------|
代码如下,需要以管理员权限允许才能执行dsmod:
#coding=utf-8 import os def 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 file for 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)] = v print ‘*‘*10,dic2 for i in dic2: # print ‘i=%s‘ %i,‘dic2[i]=%s‘ %dic2[i] if dn==‘‘:pass else:CUD(i.strip(),dic2[i])
本文出自 “七月” 博客,转载请与作者联系!
原文地址:http://374721.blog.51cto.com/364721/1735191