Advertisement
timsstuff

Create-ADUsers.ps1

Oct 23rd, 2015
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param(
  3.     [ValidateScript({Test-Path $_ -PathType Leaf})]
  4.     [Parameter(Mandatory=$True,Position=1)][string]$CSV
  5. )
  6.  
  7. Import-Module ActiveDirectory
  8.  
  9. $start = Get-Date
  10. $domain = Get-ADDomain
  11. $addomain = $domain.Forest
  12. $domaindn = $domain.DistinguishedName
  13. $logfile = ".\CreateUsers.log"
  14. $nl = "`r`n"
  15. $ou = "OU=Users,OU=Exchange,$domaindn"
  16. $pw = ConvertTo-SecureString -AsPlainText "P@55w0rd" -force
  17.  
  18.  
  19. function writelog([string]$message) {
  20.     $timestamp = Get-Date
  21.     $fc = "White"
  22.     if($message.contains("ERROR")) {$fc ="Red"}
  23.     Add-content $logfile -value "$timestamp : $message"
  24.     Write-Host $message -ForegroundColor $fc
  25. }
  26.  
  27. writelog "Importing users from $csv at $start"
  28. Write-Host $nl
  29. $loop = 0
  30.  
  31. Import-Csv $csv | ForEach-Object {
  32.     Set-Variable -Name testu -Value $null -Scope 0
  33.     $email = $_.PrimarySmtpAddress
  34.    
  35.     $emailsplit = $email.Split("@")
  36.     $username = $emailsplit[0].ToLower()
  37.     $domain = $emailsplit[1]
  38.     $flname = $username.Split(".")
  39.     $upn = $username + "@" + $addomain
  40.     If($flname.Count -eq 3) {
  41.         $FName = (Get-Culture).TextInfo.ToTitleCase($flname[0].ToLower())
  42.         $MName = $flname[1].ToUpper()
  43.         $LName = (Get-Culture).TextInfo.ToTitleCase($flname[2].ToLower())
  44.         $DisplayName = "$LName, $FName $MName  Contoso"
  45.     }
  46.     ElseIf($flname.Count -eq 2) {
  47.         $FName = (Get-Culture).TextInfo.ToTitleCase($flname[0].ToLower())
  48.         $LName = (Get-Culture).TextInfo.ToTitleCase($flname[1].ToLower())
  49.         $DisplayName = "$LName, $FName   Contoso"
  50.     }
  51.     else {
  52.         $FName = (Get-Culture).TextInfo.ToTitleCase($flname[0].ToLower())
  53.         $LName = ""
  54.         $DisplayName = (Get-Culture).TextInfo.ToTitleCase($flname[0].ToLower())
  55.     }
  56.     $testu = Get-ADUser -ldapfilter "(samaccountname=$username)"
  57.     $pre2000 = $username
  58.     if($pre2000.length -gt 20) { $pre2000 = $pre2000.Substring(0,20) }
  59.  
  60.     if($testu -eq $null) {
  61.         writelog "Creating user $DisplayName ($username)"
  62.         New-ADUser -SamAccountName $pre2000 -name $DisplayName -AccountPassword $pw -Path $ou -GivenName $FName `
  63.         -Surname $LName -DisplayName $DisplayName -EmailAddress $email -UserPrincipalName $upn -Enabled $true -ErrorAction Stop
  64.     }
  65.     else {
  66.         #writelog "User  $DisplayName ($username) already exists, skipping."
  67.     }
  68.     $loop += 1
  69.     #if ($loop -eq 5) { break }
  70. }
  71.  
  72. $end = Get-Date
  73. $elapsed = $end - $start
  74. Write-Host $nl
  75. writelog "Finished import of $loop users at $end ($elapsed)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement