Advertisement
Guest User

Untitled

a guest
Oct 5th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #!powershell
  2.  
  3. import-module ActiveDirectory
  4.  
  5. $Firstname = Read-Host -Prompt "Input the user's First Name here"
  6. $LastName = Read-Host -Prompt "Input the user's Last Name here"
  7. #user to break down what location the user is at and how with OU placment
  8. $loc = Read-Host -Prompt "User's Main Location"
  9.  
  10. #New username Vaildation
  11. while(1) {
  12. $prompt = Read-Host "Input Username"
  13. try { $user = Get-ADUser $prompt }
  14. catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
  15. $user = $prompt
  16. break
  17. }
  18. "User '$prompt' already exists!"
  19. }
  20.  
  21. "User to be created: $user"
  22.  
  23. #Role To mirror - These are user accounts in AD with predefined groups.
  24. $clonedusername = Read-Host -Prompt "Input the username to clone group memberships from (Marketing,Accounting)"
  25.  
  26. if ($clonedusername -eq "Marketing"){
  27. $OU = "OU=$Loc,OU=Sales,OU=Marketing,OU=Company Structure,DC=Contoso,DC=com"}
  28. ElseIf ($clonedusername -eq "Accounting"){
  29. $OU = "OU=$Loc,OU=Administration,OU=Accounting,OU=Company Structure,DC=Contoso,DC=com"}
  30.  
  31.  
  32. #Password Generation
  33. $randomgen = -join ((65..90) + (97..122) | Get-Random -Count 8 | % {[char]$_})
  34.  
  35.  
  36. #AD Fields
  37. $Desc = $Loc + " " + $clonedusername
  38. $Fullname = $Firstname + "` " + $LastName
  39. $samaccountname = $user.ToLower()
  40. $email = $samaccountname + "@Contoso.Com" #Change DOMAIN.COM to your actual domain if creating email addresses. Email address is compiled from Username + "@DOMAIN.COM"
  41. $password = $randomgen + "1!"
  42. $upn = "$samaccountname@Contoso.com" #Change DOMAIN.COM to your actual domain or respective domain for the UPN
  43.  
  44.  
  45.  
  46. #Create New Users' Account
  47. New-ADUser -Name $Fullname -SamAccountName "$samaccountname" -GivenName "$FirstName" -Surname "$LastName" -AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -DisplayName "$Fullname" -Path "$OU" -UserPrincipalName "$upn" -Title "$title" -EmailAddress "$email" -Enabled $true -Description "$desc"
  48.  
  49. #Queries the memberof properties of the specified user to clone, Selects the memberof properties of the user to clone, Adds the newly created user to all of the same groups
  50. Get-ADUser -Identity $clonedusername -Properties Memberof |
  51. Select-Object -ExpandProperty memberof |
  52. Add-ADGroupMember -Members $samaccountname
  53.  
  54.  
  55.  
  56.  
  57. #Outputs user's details
  58. Write-Host *****************************
  59. Write-Host "Your user has been created"
  60. Write-Host "Full Name: $Fullname"
  61. Write-Host "Username: $samaccountname"
  62. Write-Host "Email Address: $email"
  63. Write-Host "The user's permissions have been copied from: $clonedusername"
  64. Write-Host "Password: $password"
  65. Write-Host "OU Path: $ou"
  66. Write-Host *****************************
  67.  
  68.  
  69.  
  70. ##$DateStamp = get-date -uformat "%Y-%m-%d"
  71. ##Logging - Out-File -FilePath C:\Users\Desktop\$samaccountname-$dateStamp.log -Confirm -Encoding ascii
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement