Advertisement
Guest User

Untitled

a guest
Sep 5th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. ##***FUNCTIONS***##
  2.  
  3. ##Create The mailbox
  4. Function Setup-Mailbox{
  5. Clear-Host
  6. if($global:company -eq "***"){
  7. $cuser = $global:firstname + ' ' + $global:lastname
  8. Write-Host "The current Selected OU is $global:ou"
  9. Read-Host -Prompt "Hit enter if this is correct. Otherwise close and start over"
  10. New-Mailbox `
  11. -Name $cuser `
  12. -Alias $global:calias `
  13. -UserPrincipalName $global:userprinciplename `
  14. -SamAccountName $global:samaccountname `
  15. -FirstName $global:firstname `
  16. -LastName $global:lastname `
  17. -Password $global:Password `
  18. -ResetPasswordOnNextLogon $global:false `
  19. -Database $global:database `
  20. -OrganizationalUnit $global:ou
  21. }
  22. elseif($global:company -eq "***"){
  23. $cuser = $global:firstname + ' ' + $global:lastname
  24. Write-Host "The current Selected OU is $global:ou"
  25. Read-Host -Prompt "Hit enter if this is correct. Otherwise close and start over"
  26. New-Mailbox `
  27. -Name $cuser `
  28. -Alias $global:calias `
  29. -UserPrincipalName $global:userprinciplename `
  30. -SamAccountName $global:samaccountname `
  31. -FirstName $global:firstname `
  32. -LastName $global:lastname `
  33. -Password $global:Password `
  34. -ResetPasswordOnNextLogon $global:false `
  35. -Database $global:database `
  36. -OrganizationalUnit $global:ou
  37. }
  38. }
  39.  
  40. ##Select the database to store the mailbox. This will switch between the two for multiple users
  41. Function Select-Database{
  42. if ($global:lastdb -eq 1){$global:database = '****\First Storage Group\Mailbox Database'
  43. $global:lastdb = 0
  44. }
  45. elseif($global:lastdb -eq 0){$global:database = '****\Second Storage Group\Mailbox Database'
  46. $global:lastdb = 1
  47. }
  48. }
  49.  
  50. ##Get the info needed to make a new user
  51. Function Get-UserInfo{
  52. $global:firstname = Read-Host -Prompt "Enter Users First Name: "
  53. $global:lastname = Read-Host -Prompt "Enter Users Last Name: "
  54. $global:samaccountname = $global:firstname.ToLower() + $global:lastname.Substring(0,1).ToLower()
  55. $global:calias = $global:firstname.ToLower() + "." + $global:lastname.ToLower()
  56. $global:caliasativ = $global:firstname.Substring(0,1).ToLower() + $global:lastname.ToLower()
  57. $global:userprinciplename = $global:samaccountname + '@***'
  58. $global:company = Read-Host -Prompt "Company Name: "
  59. $global:location = Read-Host -Prompt "Omaha, Florida, Boston, or Remote: "
  60. $global:office = $global:location
  61. $global:department = Read-Host -Prompt "Enter Users Department: "
  62. $global:title = Read-Host -Prompt "Enter Users Title: "
  63. $global:manager = Read-Host -Prompt "Enter Users Manager: "
  64. if($global:company -eq "***"){
  65. $phonel4 = Read-Host -Prompt "Enter last 4 phone digits: "
  66. $global:phone = "402-778-" + $phonel4
  67. }
  68. elseif($global:company -eq "****"){
  69. $phonel4 = Read-Host -Prompt "Enter 4 digit Jive extension: "
  70. $global:phone = "(508) 655-0342 x" + $phonel4
  71. }
  72. Clear-Host
  73. Write-Host "First Name: " $global:firstname
  74. Write-Host "Last Name: " $global:lastname
  75. Write-Host "Login Name: " $global:samaccountname
  76. Write-Host "Principle Name: " $global:userprinciplename
  77. Write-Host "Location: " $global:location
  78. Write-Host "Office: " $global:office
  79. Write-Host "Title: " $global:title
  80. Write-Host "Department: " $global:department
  81. Write-Host "Company: " $global:company
  82. Write-Host "Manager: " $global:manager
  83. Write-Host "Phone: " $global:phone
  84. if ($global:company -eq "***"){Write-Host "Email: " "$global:calias@***.com"}
  85. elseif ($global:company -eq "***"){Write-Host "Email: " "$global:caliasativ@***.com"}
  86. Read-Host -Prompt "If this info is correct hit enter. Otherwise close and startover"
  87. }
  88.  
  89. ##Finish setting up the AD Account
  90. Function Finish-Account{
  91. #Finish Account Settings
  92. if ($global:company -eq "***"){$global:description = $global:title}
  93. elseif ($global:company -eq "****"){$global:description = $global:company + " " + $global:location + " | " + $global:department.TrimStart("**** -") + " | " + $global:title}
  94. Set-ADUser `
  95. -Identity $global:samaccountname `
  96. -Office $global:office `
  97. -Title $global:title `
  98. -Description $global:description `
  99. -Department $global:department `
  100. -Company $global:company `
  101. -Manager $global:manager `
  102. -OfficePhone $global:phone `
  103. # Protect from accdential delete
  104. Get-ADUser $global:samaccountname | Set-ADObject -ProtectedFromAccidentalDeletion $true
  105. # Rename account to match naming policy.
  106. if($global:company -eq "***"){
  107. Set Mailbox settings
  108. Set-CASMailbox $global:calias -ActiveSyncEnabled $false -OWAEnabled $false -PopEnabled $false -ImapEnabled $false
  109. #fix email addresses
  110. Set-Mailbox $global:calias -Alias $global:samaccountname -EmailAddressPolicyEnabled $false -EmailAddresses SMTP:$global:calias@americantitleinc.com,smtp:$global:samaccountname@americantitleinc.com
  111. }
  112. elseif($global:company -eq "***"){
  113. Set-CASMailbox $global:calias -ActiveSyncEnabled $false -OWAEnabled $false -PopEnabled $false -ImapEnabled $false
  114. #fix email addresses
  115. Set-Mailbox $global:calias -Alias $global:samaccountname -EmailAddressPolicyEnabled $false -EmailAddresses SMTP:$global:caliasativ@***.com,smtp:$global:samaccountname@***.com
  116. }
  117. }
  118.  
  119.  
  120. ##Function to run the account setup again
  121. Function RunAgain{
  122. $title = "Run Again?"
  123. $message = "Do you want to create another account?"
  124. $message
  125.  
  126. $choice1 = New-Object System.Management.Automation.Host.ChoiceDescription 'Yes', "Create another user"
  127.  
  128. $choice2 = New-Object System.Management.Automation.Host.ChoiceDescription 'No', "end script"
  129.  
  130. $options = [System.Management.Automation.Host.ChoiceDescription[]]($choice1, $choice2)
  131.  
  132. $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  133.  
  134. switch ($result)
  135. {
  136. 0 {
  137. Get-UserInfo
  138. Write-Host "Gathered Info"
  139. #Verify-Info
  140. ##Check if Name exists and if so ask for new name
  141. if(Check-ADAccount = $true){
  142. $global:samaccountname = Read-Host -Prompt "$global:samaccountname Is already taken. Please Select a new login name: "
  143. $global:userprinciplename = $global:samaccountname + '@***'
  144. }
  145. Write-Host "Verified accountname"
  146. Select-Database
  147. Write-Host "Selected Database " + $global:database
  148. Get-OU
  149. Write-Host "Set OU to " + $global:ou
  150. Setup-Mailbox
  151. Write-Host "Setup Mailbox"
  152. Start-Sleep -s 15
  153. Finish-Account
  154. Write-Host "Finished Account"
  155. RunAgain
  156. }
  157. 1 {
  158. exit
  159. }
  160. }
  161. }
  162. <#
  163. Function Verify-Info{
  164. Compile-Table
  165. $title = "Verify"
  166. $message = "Is the above Info Correct?"
  167. $message
  168.  
  169. $choice1 = New-Object System.Management.Automation.Host.ChoiceDescription 'Yes', "Confirm User"
  170.  
  171. $choice2 = New-Object System.Management.Automation.Host.ChoiceDescription 'No', "Start over"
  172.  
  173. $options = [System.Management.Automation.Host.ChoiceDescription[]]($choice1, $choice2)
  174.  
  175. $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  176.  
  177. switch ($result)
  178. {
  179. 0 {
  180. ##Do nothing to continue script
  181. }
  182. 1 {
  183. ##ReRun Script
  184. RunAgain
  185. }
  186. }
  187. }
  188. #>
  189.  
  190. ##Function to check if account name already exists and to prompt for new name if exists
  191. Function Check-ADAccount($exists){
  192. $Usercheck = Get-ADUser -LDAPFilter "(sAMAccountName=$global:samaccountname)"
  193. If ($Usercheck -eq $Null) {$exists = $false}
  194. Else {$exists = $true}
  195. $exists
  196. return
  197. }
  198.  
  199.  
  200. ##Global Variables
  201. $global:caliasativ = ""
  202. $global:ou = ""
  203. $global:location = ""
  204. $global:caliasati = ""
  205. $global:firstname = ""
  206. $global:lastname = ""
  207. $global:samaccountname = ""
  208. $global:userprinciplename = ""
  209. $global:office = ""
  210. $global:title = ""
  211. $global:description
  212. $global:department = ""
  213. $global:company = ""
  214. $global:manager = ""
  215. $global:phone = ""
  216. $global:lastdb = 0
  217. $global:database = ""
  218. $global:password = ConvertTo-SecureString -String "****" -AsPlainText -Force
  219.  
  220. ##import needed modules
  221. Import-Module ActiveDirectory
  222. add-pssnapin microsoft.exchange.management.powershell.admin #Exchange 2007
  223. #Add-PSSnapin microsoft.exchange.management.powershell.snapin #Exchange 2013
  224.  
  225. ##Start Process
  226. Get-UserInfo
  227. Write-Host "Gathered Info"
  228. #Verify-Info
  229. ##Check if Name exists and if so ask for new name
  230. if(Check-ADAccount = $true){
  231. $global:samaccountname = Read-Host -Prompt "$global:samaccountname Is already taken. Please Select a new login name: "
  232. $global:userprinciplename = $global:samaccountname + '@***'
  233. }
  234. Write-Host "Verified accountname"
  235. Select-Database
  236. Write-Host "Selected Database $global:database"
  237. Get-OU
  238. Write-Host "Set OU to $global:ou"
  239. Setup-Mailbox
  240. Write-Host "Setup Mailbox"
  241. Start-Sleep -s 15
  242. Finish-Account
  243. Write-Host "Finished Account"
  244. RunAgain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement