Advertisement
Guest User

makeusers.py

a guest
Aug 4th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. import random
  2. domainname = raw_input("Domain Name: ")
  3. suffix = raw_input("Domain Suffix: ")
  4.  
  5. alpha = ""
  6. num = ""
  7. alpha += "abcdefghijklmnopqrstuvwxyz"
  8. alpha += "abcdefghijklmnopqrstuvwxyz".upper()
  9. alpha += "1234567890"
  10. num += "1234567890"
  11. passwords = ""
  12. passwords2 = []
  13. onepassword = False
  14.  
  15.  
  16. print 'format: FirstName,LastName,Organisational Unit'
  17. usersfile = raw_input("CSV file for users: ")
  18. f=open(usersfile,'r')
  19. data=f.read().splitlines()
  20. f.close()
  21.  
  22.  
  23. seconddata="""
  24. $Rights = [System.Security.AccessControl.FileSystemRights]"Modify,ReadAndExecute,ListDirectory,Read,Write"
  25. $InheritanceFlag = @([System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.InheritanceFlags]::ObjectInherit)
  26. $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
  27. $objType =[System.Security.AccessControl.AccessControlType]::Allow
  28. """
  29.  
  30. alldata="echo off\ncls\n"
  31. ous=[]
  32. d=raw_input("Site: ")
  33. for i in data:
  34.     i=i.split(',')[2]
  35.     if not i in ous:
  36.         alldata+="DSADD OU \"OU="+i+",OU="+d+",DC="+domainname+",DC="+suffix+"\"\n"
  37.         ous.append(i)
  38.         print 'Added OU: '+i
  39.  
  40. #if raw_input("Add OUs? (y/n)").upper() == 'Y':
  41. #    print "Enter '#' to finish."
  42. #    ouname = ''
  43. #    while ouname != '#':
  44. #        ouname = raw_input("OU Name: ")
  45. #        if ouname != '#':
  46. #            alldata+="DSADD OU OU="+ouname+",DC="+domainname+",DC="+suffix+"\n"
  47. print "Enter password for all users,"
  48. print "Leave blank for random passwords."
  49. passwords = raw_input("Password: ")
  50. if passwords == "":
  51.     passwordexpire = 'no -mustchpwd yes '
  52.     lenth = raw_input("Password Lentgh: ")
  53.     for i in range(0,len(data)):
  54.         while True:
  55.             tmp=""
  56.             for ii in range(0,int(lenth)):
  57.                 tmp += alpha[random.randint(0,len(alpha)-1)]
  58.             #print tmp
  59.             t2=False
  60.             for b in num:
  61.                 if b in tmp:
  62.                     t2=True
  63.             if t2:
  64.                 passwords2.append(tmp)
  65.                 break
  66. else:
  67.     passwordexpire = 'yes'
  68.     onepassword=True
  69. print "EG: \\\\192.168.1.1\\HOMEDIR"
  70. homedir = raw_input("Home Dir Path:")+'\\'+d
  71.  
  72. count = 0
  73. passworddata=""
  74. for i in data:
  75.     i2=i.split(',')
  76.     firstname=i2[0]
  77.     lastname=i2[1]
  78.     group=i2[2]
  79.     tmp='\n'
  80.  
  81.     if onepassword:
  82.         passwd = passwords
  83.     else:
  84.         passwd = passwords2[count]
  85.         count += 1
  86.     passworddata += firstname+lastname[0]+':'+passwd+'\n'
  87.     tmp += 'DSADD USER "CN=' + firstname + ' ' + lastname + ',OU=' + group + ',OU=' + d + ',DC='+domainname+',DC='+suffix+'" -disabled no -pwd '+passwd+\
  88.            ' -samid ' +firstname + lastname[0] + ' -upn '+firstname + lastname[0] +'@'+domainname+'.'+suffix+' -fn '+ firstname +' -ln ' + lastname +\
  89.            ' -display "'+firstname+' '+lastname+'" -pwdneverexpires '+passwordexpire+' -hmdir "'+homedir+'\\'+group+'\\'+firstname+'-' +\
  90.            lastname+'" -hmdrv H:\n'
  91.    
  92.     alldata += tmp
  93.  
  94.     seconddata +='$NewFolder = New-Item -Path "'+homedir+'\\'+group+'" -Name '+firstname+'-'+lastname+' -ItemType "Directory"\n'
  95.     seconddata +='$objUser = New-Object System.Security.Principal.NTAccount "'+domainname+'.'+suffix+'\\'+firstname+lastname[0]+'"\n'
  96.     seconddata +='$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $Rights, $InheritanceFlag, $PropagationFlag, $objType)\n'
  97.     seconddata +='$ACL = Get-Acl -Path $NewFolder\n $ACL.AddAccessRule($objACE)\nSet-ACL -Path $NewFolder.FullName -AclObject $ACL\n\n'
  98.  
  99. f=open('files/users.bat','w')
  100. f.write(alldata)
  101. f.close()
  102.  
  103. f=open('files/permissions.ps1','w')
  104. f.write(seconddata)
  105. f.close()
  106.  
  107. f=open('files/userspasswords.txt','w')
  108. f.write(passworddata)
  109. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement