Advertisement
Guest User

Untitled

a guest
Nov 10th, 2017
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #Set the title of the window.
  2. $host.ui.RawUI.WindowTitle = "New User Creation Script"
  3.  
  4. #Give the user a quick description
  5. Write-Host
  6. Write-Host *******************************************************************************************
  7. Write-Host This script will create a new AD user, sync the user to Office 365, and license the user.
  8. Write-Host To run this script, you will need the ActiveDirectory and MSOnline PowerShell modules
  9. Write-Host *******************************************************************************************
  10. Write-Host
  11.  
  12. #Import needed module.
  13. Import-Module ActiveDirectory
  14.  
  15. #Prompt for needed information to use as variables below
  16. $fullname = Read-Host "Enter Full Name"
  17. $first = Read-Host "First name"
  18. $last = Read-Host "Last name"
  19. $user = Read-Host "Username"
  20. $title = Read-Host "Title"
  21. get-adorganizationalunit -Filter * -Properties * -SearchBase "OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM" | Select-Object -Property Name
  22. $department = Read-Host "Enter department from above list"
  23. $manager = Read-Host "Manager userame"
  24. $srcuser = Read-Host "Username to copy"
  25. get-ADOrganizationalUnit -Filter * -Properties * -SearchBase "OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM" | Select-Object -Property Name, DistinguishedName | Format-Table -Auto
  26. $OU = Read-Host "Select OU from above list"
  27.  
  28. #Create a new user with the provided information and some static information
  29. New-ADUser -Name "$fullname" -GivenName "$first" -Surname "$last" -DisplayName "$first $last" -Description "$title" -EmailAddress "$first.$last@bnztest.com" -SamAccountName "$user" -UserPrincipalName "$user@bnztest.com" -Manager "$manager" -Title "$title" -AccountPassword (Read-Host -AsSecureString "Please enter the desired password") -Enabled $true -Path $OU
  30.  
  31.  
  32. #Add multiple ProxyAddresses if needed
  33. Set-ADUser "$user" -Add @{ProxyAddresses="smtp:$first.$last@bnztest.com"}
  34.  
  35.  
  36. #Copy group membership of the source user above
  37. Get-ADUser -Identity "$srcuser" -Properties memberof |
  38. Select-Object -ExpandProperty memberof |
  39. Add-ADGroupMember -Members "$user" -PassThru |
  40. Select-Object -Property SamAccountName >$null
  41. Write-Host 'CHECK AD REPLICATION BEFORE CONTINUING!'
  42. pause
  43.  
  44. #Sync user to Office 365 using Dir Sync on a remote server
  45. Import-Module ADSync
  46. Start-ADSyncSyncCycle -PolicyType Initial
  47. Start-Sleep -s 100
  48.  
  49.  
  50. #License user in Office 365
  51. $AdminName = "admin@testbnz.onmicrosoft.com"
  52. $Pass = Get-Content "C:UsersAdministratorDesktopCreateUserCred.txt" | ConvertTo-SecureString
  53. $Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
  54. Import-Module MSOnline
  55. Connect-MsolService -Credential $cred
  56. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
  57. Import-PSSession $Session
  58. Start-Sleep -s 15
  59. Set-MsolUser -UserPrincipalName "$user@bnztest.com" -UsageLocation 'US'
  60. Set-MsolUserLicense -UserPrincipalName "$user@bnztest.com" -AddLicenses "TESTBNZ:O365_BUSINESS_PREMIUM"
  61. Start-Sleep 90
  62. Write-Host 'ENSURE THERE ARE NO ERRORS AND THAT THE MAILBOX HAS BEEN CREATED BEFORE CONTINUING!'
  63. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement