Advertisement
Upbeatjunk

Add AD Users/Sync Office 365

Mar 23rd, 2016
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. $UserCredential = Get-Credential
  2.  
  3. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
  4.  
  5. Import-PSSession $Session
  6. $first = read-Host 'First Name:'
  7. $last = read-Host 'Last Name:'
  8. $Description = read-host 'Title:'
  9. $Office = read-Host 'Department:'
  10. $Phone = read-Host 'Cell or Extension Number:'
  11. $un = read-Host 'Username:'
  12. $PlainPassword = Read-Host 'Secure Password with at least 8 characters, one number or symbol. Case sensitive:'
  13. $pw = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force
  14. $Name = $first + ' ' + $last
  15. $homedr = 'P:'
  16. $Homedir = '\server\userdirs\' + $un
  17.  
  18. new-ADUser $name -Enabled $true -AccountPassword $pw -Path 'OU=whatever,DC=domain,DC=local' -Department $Office -Description $Description -DisplayName $name -HomeDirectory $Homedir -Manager $Manager -Office $Office -ScriptPath $logon -Title $Description -OfficePhone $Phone -SamAccountName $un -GivenName $first -Surname $last -OtherAttributes @{userprincipalname="$un@domain.local";mail="$un@domain.com";proxyaddresses="SMTP:$un@domain.com";targetaddress="SMTP:$un@domain.onmicrosoft.com";mobile="$Phone"} -passwordneverexpires 1
  19.  
  20. set-aduser -Identity $un -homedrive $homedr add-ADGroupMember 'DomainGroup' -Members $un add-ADGroupMember $Office -Members $un
  21.  
  22. $message = "Please select an option. Use UPPER CASE LETTER!"
  23. $city1 = New-Object System.Management.Automation.Host.ChoiceDescription "&city1","Add City1 info"
  24. $city2 = New-Object System.Management.Automation.Host.ChoiceDescription "&city2","Add City2 info"
  25. $field = New-Object System.Management.Automation.Host.ChoiceDescription "&field","Add field info"
  26. $options = [System.Management.Automation.Host.ChoiceDescription[]]($city1,$city2,$field)
  27. $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  28.  
  29. switch ($result) {
  30. 0 {
  31. set-ADUser $un -City "City1" -Company "Company Name" -PostalCode "55555" -State "ST" -StreetAddress "Blah street" -Title $Description -OfficePhone $Phone
  32. }
  33. 1 {
  34. set-ADUser $un -City "City2" -Company "Company Name" -PostalCode "55555" -State "ST" -StreetAddress "Blah Street2" -Title $Description -OfficePhone $Phone
  35. }
  36. 2 {
  37. "Field"
  38. }
  39. }
  40.  
  41. set-aduser $un -Enabled $true
  42. $DomainControllers = Get-ADDomainController -Filter *
  43. ForEach ($DC in $DomainControllers.Name) {
  44. Write-Host "Processing for "$DC -ForegroundColor Green
  45. If ($Mode -eq "ExtraSuper") {
  46. REPADMIN /kcc $DC REPADMIN /syncall /A /e /q $DC
  47. }
  48. Else {
  49. REPADMIN /syncall $DC "dc=domain,dc=local" /d /e /q
  50. }
  51. }
  52.  
  53. Invoke-Command -ComputerName machine.domain.local -ScriptBlock {import-module dirsync;Start-onlinecoexistencesync}
  54. Start-Sleep -s 360 connect-msolservice -credential $UserCredential
  55. Write-host "Setting Office 365 Account Password"
  56.  
  57. Set-MsolUserPrincipalName -newuserprincipalname $un@domain.com -userprincipalname $un@domain.onmicrosoft.com
  58.  
  59. Set-MsolUser -UserPrincipalName "$un@domain.com" -UsageLocation US Set-MsolUserLicense -UserPrincipalName "$un@domain.com" -AddLicenses domain:EXCHANGESTANDARD
  60.  
  61. Set-MsolUser -UserPrincipalName "$un@domain.com" -StrongPasswordRequired $False start-sleep -s 90
  62.  
  63. Set-MsolUserPassword -UserPrincipalName "$un@domain.com" -NewPassword $PlainPassword -ForceChangePassword $false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement