bblades262

CreateUsersfromCSV

Jul 28th, 2016 (edited)
1,565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.   1. Import from CSV file
  3.   2. Create Users in AD
  4.   3. Add users to groups
  5.  
  6. #>
  7. #Import users to $Users variable
  8. $Users = Import-Csv -Path "C:\temp\newhires.csv"
  9. $ADServer = DC.DOMAIN.TLD
  10.  
  11.  
  12. #Create an empty Arraylist
  13. $ExistingUsers = New-Object System.Collections.ArrayList($null)
  14. #Create timer variable
  15. $Sleepseconds = 5
  16.  
  17. foreach ($User in $Users) #Start work for each user  
  18. {
  19.   #Create variables to work with for each row in the CSV file
  20.   $Displayname = $User.Firstname + " " + $User.Lastname
  21.   $UserFirstname = $User.Firstname
  22.   $UserMI = $User.MI
  23.   $UserLastname = $User.Lastname
  24.   $Initials = $User.Initials
  25.   $OU = "OU=USER OU,DC=DOMAIN,DC=TLD"
  26.   $SAM = $UserFirstname.substring(0,1) + $UserMI + $UserLastname
  27.   $UPN = $UserFirstname.substring(0,1) + $UserMI + $UserLastname + "@PUBLICDOMAIN.TLD"
  28.   $Password = "NewUserPass" + $User.Initials
  29.   $StreetAddress = $User.StreetAddress
  30.   $City = $User.City
  31.   $State = $User.State
  32.   $PostalCode = $User.PostalCode
  33.   $Department = $User.Department
  34.   $Manager = $User.Manager
  35.   $Title = $User.Title
  36.   $MobilePhone = $User.MobilePhone
  37.   $OfficePhone = $User.OfficePhone
  38.   $ADGroup1 = $User.ADGroup1
  39.   $ADGroup2 = $User.ADGroup2
  40.   #Test if user exists already
  41.   $TryUser = Get-ADUser -LDAPFilter "(sAMAccountName=$SAM)"
  42.   If ($TryUser -eq $null) { #Only add user if the user doesn't already exist
  43.     New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -EmailAddress $UPN -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -StreetAddress $StreetAddress -City $City -State $State -PostalCode $PostalCode -MobilePhone $MobilePhone -OfficePhone $OfficePhone -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false -Department $Department -Manager $Manager -Title $Title -server $ADServer
  44.     Do { #Wait for user propogation, then set additional settings
  45.       Write-Host "Checking if User has been created"
  46.       Start-Sleep -Seconds $Sleepseconds
  47.       $ADUserCreated=Get-ADUser $SAM
  48.     } While ($ADUserCreated -eq $Null)
  49.     Set-ADUser -Identity $SAM -Add @{'Proxyaddresses'=@("SMTP:"+$UPN)}
  50.     Set-ADUser -Identity $SAM -Add @{'Proxyaddresses'=@("smtp:"+$SAM+"@PUBLICDOMAIN.onmicrosoft.com")}
  51.     Set-ADUser -Identity $SAM -Add @{'Proxyaddresses'=@("smtp:"+$SAM+"@PUBLICDOMAIN.mail.onmicrosoft.com")}
  52.     Set-ADUser -Identity $SAM -Add @{'TargetAddress'=@("SMTP:"+$SAM+"@PUBLICDOMAIN.mail.onmicrosoft.com")}
  53.     Add-ADGroupMember -Identity $ADGroup1 -Members $SAM
  54.     Add-ADGroupMember -Identity $ADGroup2 -Members $SAM
  55.   } Else { #Add user to list of Existing users
  56.       $ExistingUsers.add($SAM)
  57.     }
  58. } # End foreach $User block
  59.  
  60.  
  61. <#
  62. Sync AD to AAD
  63. #>
  64.  
  65. invoke-command -computername SERVER -ScriptBlock {Start-ADSyncSyncCycle -PolicyType Delta}
  66.  
  67. <#
  68. 1. Set location
  69. 2. Assign license
  70. 2. Add to group
  71. 3. Disable POP3 and IMAP
  72. #>
  73.  
  74. #Connect to MSOnline
  75. import-module MSOnline
  76. $MSOLCred = Get-Credential -UserName "AZUREADMIN@PUBLICDOMAIN.TLD" -Message "MSOL/Office365 Login"
  77. connect-msolservice -credential $MSOLCred
  78. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $MSOLCred -Authentication Basic -AllowRedirection
  79. Import-PSSession $Session
  80.  
  81. foreach ($User in $Users) {#Begin AAD Licensing assignment
  82.   $UserFirstname = $User.Firstname
  83.   $MSOLUPN = $UserFirstname.substring(0,1) + $User.MI + $User.LastName + "PUBLICDOMAIN.TLD"
  84.   Do { # Wait for AAD sync
  85.     Write-Host "Checking if user synced to Azure"
  86.     Start-Sleep -Seconds $Sleepseconds
  87.     $UserCreated = Get-MsolUser -UserPrincipalName $MSOLUPN -ErrorAction SilentlyContinue
  88.   } While ($UserCreated -eq $Null)
  89.   $MSOLSKU = "PUBLICDOMAIN:ENTERPRISEPACK"
  90.   $UserFirstName = $User.FirstName
  91.   $UserLastName = $User.LastName
  92.   $MSOLObjID = (Get-MsolUser -UserPrincipalName $MSOLUPN).objectid.tostring()
  93.   Set-MSOLUser -UserPrincipalName $MSOLUPN -UsageLocation US
  94.   Set-MSOLUserLicense -ObjectId $MSOLObjID -AddLicenses $MSOLSKU
  95. } # End AAD Licensing assignment
  96.  
  97.  
  98. foreach ($User in $Users) { # Begin Exchange online assignments
  99.   $UserFirstname = $User.Firstname
  100.   $MSOLUPN = $UserFirstname.substring(0,1) + $User.MI + $User.LastName + "PUBLICDOMAIN.TLD"
  101.   $checkifmailboxexists = get-mailbox $MSOLUPN -erroraction silentlycontinue
  102.   Do { #Wait for mailbox to be created
  103.      $checkifmailboxexists = get-mailbox $MSOLUPN -erroraction silentlycontinue
  104.      Write-Host "Checking if mailbox has been created"
  105.      Start-Sleep -Seconds $Sleepseconds
  106.   } While ($checkifmailboxexists -eq $Null)
  107.   Set-CASMailbox $MSOLUPN -PopEnabled $FALSE -ImapEnabled $FALSE -erroraction silentlycontinue
  108. #  Add-DistributionGroupMember -Identity "ALLUSERS GROUP" -Member $MSOLUPN -erroraction silentlycontinue
  109. #  Write-Output "$MSOLUPN Mailbox created. POP3 and IMAP disabled. User added to Distribution List"
  110. } # End Exhange online assignments block
  111.  
  112. foreach ($User in $Users) { # Ticket info output
  113.   $UserFirstname = $User.Firstname
  114.   $UserLastname = $User.Lastname
  115.   $UserMI = $User.MI
  116.   $UPN = $UserFirstname.substring(0,1) + $UserMI + $UserLastname + "PUBLICDOMAIN.TLD"
  117.   $ADGroup1 = $User.ADGroup1
  118.   $ADGroup2 = $User.ADGroup2
  119.   $OfficePhone = $User.OfficePhone
  120.   Write-Output "User Created as $UPN with default password. `n`nAdded to groups: `n  $ADGroup1 `n  $ADGroup2 `n`nPhone Extension: $OfficePhone"
  121. } # End Ticket info output block
Add Comment
Please, Sign In to add comment