Advertisement
Guest User

Untitled

a guest
Sep 8th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #Set script Parameters for the inputs
  2. param (
  3. [String]$First,
  4. [String]$Last,
  5. [String]$SamAccountName
  6. )
  7.  
  8. #Set Message Strings for the Function Message-Prompt
  9. $MsgGADSSync = "Do you want to run a test GADS Sync?"
  10. $MsgGADSResultApply = "Were the sync results correct? and should we apply the sync?"
  11. $MsgCreateUser = "Do you want to create this user?"
  12. $SchoolPrompt = "Is This a High School Student [1] or an Elementary Student [2]"
  13.  
  14. #Create Functions
  15. #the message prompting function and return a value 0 or 1 (false or true repsectively)
  16. function Message-Prompt
  17. {
  18. param (
  19. [String]$MessageX
  20. )
  21.  
  22. Do{
  23. Write-Host "$messagex" -ForegroundColor Yellow
  24. Write-Host "[Y] Yes or [N] No" -ForegroundColor Cyan
  25. $choicex = Read-Host -Prompt "Select an option"
  26. } until ($choicex -eq "y" -or $choicex -eq "n" -or $choicex -eq "yes" -or $choicex -eq "no")
  27.  
  28. If ($choicex -eq "y" -or $choicex -eq "yes"){
  29. Set-Variable -Name ReturnValue -Value "1" -Scope Script
  30. }
  31. elseif ($choicex -eq "n" -or $choicex -eq "no"){
  32. Set-Variable -Name ReturnValue -Value "0" -Scope Script
  33. }
  34. else {
  35. exit
  36. }
  37. }
  38.  
  39. # Function reset return value
  40. function Reset-ReturnValue
  41. {
  42. Set-Variable -Name ReturnValue -Value 99 -Scope Script
  43. }
  44.  
  45.  
  46. #Prompts for more data
  47. $School = "Default Value Please Change"
  48. Do{
  49. Write-Host "$SchoolPrompt" -ForegroundColor Yellow
  50. Write-Host "[1] High School or [2] Elementary" -ForegroundColor Cyan
  51. $SchoolSelected = Read-Host -Prompt "Select an option"
  52. } until ($SchoolSelected -eq "1" -or $SchoolSelected -eq "2")
  53.  
  54. If ($SchoolSelected -eq "1") {
  55. Set-Variable -Name School -Value "hs_students" -Scope script
  56. }
  57. else {
  58. Set-Variable -Name School -Value "el_students" -Scope script
  59. }
  60.  
  61. #More prompts and more data
  62. $Path = Read-Host "Enter GraduationYear"
  63. $Password = Read-Host "Set User Password"
  64.  
  65. #build other information using what has already been prompted for
  66. $Fullname = "$($First) $($Last)"
  67. $UserPrincipleName = "$($SamAccountName)@mr1.local"
  68. $EmailAddress = "$($SamAccountName)@maysville.k12.mo.us"
  69. $FullPath = "OU=$($Path),OU=$($school),OU=Student_Accounts,OU=User Accounts,DC=mr1,DC=local"
  70.  
  71. #Print out what will be created
  72. Write-Host "Does the Data Look Correct?" -ForegroundColor Magenta
  73. Write-Host "$FullName" -ForegroundColor Yellow
  74. Write-host "$SamAccountName" -ForegroundColor Yellow
  75. Write-host "$EmailAddress" -ForegroundColor Yellow
  76. Write-host "$FullPath" -ForegroundColor Yellow
  77. Write-Host "`n"
  78.  
  79.  
  80. #prompt for account creation and run the results
  81. Message-Prompt $MsgCreateUser
  82. If ($ReturnValue -eq "1") {
  83. New-ADUser -GivenName $First -Surname $Last -Name $Fullname -DisplayName $FullName -SamAccountName $SamAccountName -UserPrincipalName $UserPrincipleName -EmailAddress $EmailAddress -Path $FullPath -Enabled $True #-AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -ChangePasswordAtLogon $True
  84. Add-ADGroupMember $Path $SamAccountName
  85. }
  86. else {
  87. Exit
  88. }
  89.  
  90. #Prompt and ask for the GADS portion of the script.
  91. Message-Prompt $MsgGADSSync
  92. If ($ReturnValue -eq "1") {
  93. Invoke-Command -ComputerName utilityserver -ScriptBlock {& 'c:\Program Files\Google Apps Directory Sync\sync-cmd.exe'"-c c:\gads\configurations\current_final_v1"}
  94. }
  95. else{
  96. Write-Host "Continuing On.."
  97. }
  98.  
  99. #Clear Return Value
  100. Reset-ReturnValue
  101.  
  102. #Prompt Apply GADS Sync
  103. Message-Prompt $MsgGADSResultApply
  104. If ($ReturnValue -eq "1") {
  105. Write-host "Applying Google Apps Sync In 3 seconds. Ctrl+c to quit"
  106. Start-Sleep -s 3
  107. Invoke-Command -ComputerName utilityserver -ScriptBlock {& 'c:\Program Files\Google Apps Directory Sync\sync-cmd.exe'"-c c:\gads\configurations\current_final_v1" "-a"}
  108. }
  109. else {
  110. Write-Host "Continuing on..."
  111. }
  112.  
  113. Reset-ReturnValue
  114.  
  115. #Set the password parameters based on $school variable
  116. Write-Host "Setting Password Parameters Based On $School Value"
  117. If ($School -eq "hs_students") {
  118. Set-AdAccountPassword -identity $SamAccountName -reset -newpassword (ConvertTo-SecureString $Password -AsPlainText -force) -Verbose | Set-ADUser -ChangePasswordAtLogon $True -Verbose
  119. }
  120. Else {
  121. Set-AdAccountPassword -identity $SamAccountName -reset -newpassword (ConvertTo-SecureString $Password -AsPlainText -force) -Verbose | Set-Aduser -CannotChangePassword $True -ChangePasswordAtLogon $False -PasswordNeverExpires $True -Verbose
  122. }
  123.  
  124.  
  125. Write-Host "ALL DONE!!!" -ForegroundColor Magenta
  126.  
  127. Start-Sleep -s 1
  128.  
  129. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement