gangstanthony

if_selection_trouble

Jun 23rd, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # https://www.reddit.com/r/PowerShell/comments/4peg5u/if_selection_trouble/
  2.  
  3. <#
  4.  
  5. ########################################
  6.                 Idea
  7. ########################################
  8.  
  9.     User Creation Script
  10.     Hoping for an almost 1 click solution
  11.  
  12. ########################################
  13.                 To Do
  14. ########################################
  15.  
  16.     1. Give User FP, RW, RO Permissions for necessary OU ( Write-host | Read-host )
  17.     2. Assign Microsoft Licenses based on $UserOU (New Script)
  18.     3. $UserName check exists (DONE)
  19.     4. Error Handling
  20.  
  21. ########################################
  22.           Table of Contents
  23. ########################################
  24.  
  25.     0. Create Out-Menu function
  26.     1. Importing Module
  27.         a. Verify
  28.     2. Getting Variables
  29.     3. OU Selection
  30.     4. Domain Selection
  31.     5. User Creation
  32.  
  33. #>
  34.  
  35. <#######################################
  36.          0. Create Out-Menu function
  37. ########################################>
  38.  
  39. # https://github.com/gangstanthony/PowerShell/blob/master/Out-Menu.ps1
  40.  
  41. function Out-Menu {
  42.     param (
  43.         [Parameter(
  44.             Mandatory=$true,
  45.             ValueFromPipeline=$True,
  46.             ValueFromPipelinebyPropertyName=$True)]
  47.         [object[]]$Object,
  48.         [string]$Header,
  49.         [string]$Footer,
  50.         [switch]$AllowCancel,
  51.         [switch]$AllowMultiple
  52.     )
  53.  
  54.     if ($input) {
  55.         $Object = @($input)
  56.     }
  57.  
  58.     if (!$Object) {
  59.         throw 'Must provide an object.'
  60.     }
  61.  
  62.     Write-Host ''
  63.  
  64.     do {
  65.         $prompt = New-Object System.Text.StringBuilder
  66.         switch ($true) {
  67.             {[bool]$Header -and $Header -notmatch '^(?:\s+)?$'} { $null = $prompt.Append($Header); break }
  68.             $true { $null = $prompt.Append('Choose an option') }
  69.             $AllowCancel { $null = $prompt.Append(', or enter "c" to cancel.') }
  70.             $AllowMultiple {$null = $prompt.Append("`nTo select multiple, enter numbers separated by a comma EX: 1, 2") }
  71.         }
  72.         Write-Host $prompt.ToString()
  73.  
  74.         for ($i = 0; $i -lt $Object.Count; $i++) {
  75.             Write-Host "$('{0:D2}' -f ($i+1)). $($Object[$i])"
  76.         }
  77.  
  78.         if ($Footer) {
  79.             Write-Host $Footer
  80.         }
  81.  
  82.         Write-Host ''
  83.  
  84.         if ($AllowMultiple) {
  85.             $answers = @(Read-Host).Split(',').Trim()
  86.  
  87.             if ($AllowCancel -and $answers -match 'c') {
  88.                 return
  89.             }
  90.  
  91.             $ok = $true
  92.             foreach ($ans in $answers) {
  93.                 if ($ans -in 1..$Object.Count) {
  94.                     $Object[$ans-1]
  95.                 } else {
  96.                     Write-Host 'Not an option!' -ForegroundColor Red
  97.                     Write-Host ''
  98.                     $ok = $false
  99.                 }
  100.             }
  101.         } else {
  102.             $answer = Read-Host
  103.  
  104.             if ($AllowCancel -and $answer -eq 'c') {
  105.                 return
  106.             }
  107.  
  108.             $ok = $true
  109.             if ($answer -in 1..$Object.Count) {
  110.                 $Object[$answer-1]
  111.             } else {
  112.                 Write-Host 'Not an option!' -ForegroundColor Red
  113.                 Write-Host ''
  114.                 $ok = $false
  115.             }
  116.         }
  117.     } while (!$ok)
  118. }
  119.  
  120. <#######################################
  121.          1. Importing Module
  122. ########################################>
  123.  
  124. Import-Module ActiveDirectory
  125.  
  126. ##### 1. a. Verify
  127. if (!(Get-Command New-ADUser -ea 0)) {
  128.     throw 'Problem loading ActiveDirectory module'
  129. }
  130.  
  131. <#######################################
  132.          2. Getting Variables
  133. ########################################>
  134.  
  135. <#
  136. New-ADUser property name(Powershell)    AD property on the GUI (ADAC)   LDAP attribute
  137. DisplayName                             Display name                    displayName
  138. GivenName                               First name                      givenName
  139. Initials                                Middle initials                 initials
  140. Name                                    Full name                       name
  141. OtherName                               –                             middleName
  142. SamAccountName                          User SamAccountName logon       sAMAccountName
  143. Surname                                 Last name                       sn
  144. #>
  145.  
  146. $userinfo = @{
  147.     GivenName = Read-Host 'Please enter First name'
  148.     Surname = Read-Host 'Please enter Last name'
  149.     SAMAccountName = Read-Host 'Please enter Username'
  150.     Description = Read-Host 'Please enter Users Description for Active Directory'
  151.     Title = Read-Host 'Please enter Users Job Title'
  152.     AccountPassword = Convertto-SecureString (Read-Host 'Please enter Password') -AsPlainText -Force
  153.     HomeDrive = 'H:'
  154.     Path = ''
  155.     Company = ''
  156.     EmailAddress = ''
  157.     HomeDirectory = ''
  158.     ScriptPath = ''
  159.     Enabled = $true
  160. }
  161.  
  162. $alreadyexists = Get-ADObject -Filter "samaccountname -eq $($userinfo.SAMAccountName)"
  163. if ($alreadyexists) {
  164.     Write-Warning "$($userinfo.SAMAccountName) already exists!"
  165.     Write-Output $alreadyexists
  166.     return
  167. }
  168.  
  169. $userinfo.Name = $userinfo.GivenName + ' ' + $userinfo.Surname
  170. $userinfo.DisplayName = $userinfo.Name
  171.  
  172. <#######################################
  173.           3. OU Selection
  174. ########################################>
  175.  
  176. $UserOUSel = @{
  177.     part1 = ''
  178.     part2 = ''
  179. }
  180.  
  181. $UserOUSel.part1 = Out-Menu -Object 'Staff', 'Students' -Header 'OU selection (part 1 of 2)'
  182.  
  183. switch ($UserOUSel.part1) {
  184.     'Staff' {
  185.         $UserOUSel.part2 = Out-Menu -Object 'Administration', 'Childcare', 'NonTeaching', 'RBCChurch', 'Teachers', 'Library' -Header 'OU selection Staff (part 2 of 2)'
  186.         $userinfo.HomeDirectory = Join-Path '\\contoso\staff$\' $userinfo.SAMAccountName
  187.     }
  188.  
  189.     'Students' {
  190.         $UserOUSel.part2 = Out-Menu -Object (1..12 | % {"Year $_"}) -Header 'OU selection Students (part 2 of 2)'
  191.         $userinfo.HomeDirectory = Join-Path '\\contoso\students\' $userinfo.SAMAccountName
  192.     }
  193. }
  194.  
  195. switch -Regex ($UserOUSel.part2) {
  196.     # Staff Variables depending on $UserOUSel
  197.     '^Administration$' {
  198.         $userinfo.Path = 'OU=Administration,OU=staff,OU=User'
  199.         $userinfo.Company = 'college'
  200.         $userinfo.Department = 'Aohgida bohgida'
  201.         $userinfo.ScriptPath = 'Admin.cmd'
  202.     }
  203.  
  204.     '^Childcare$' {
  205.         $userinfo.Path = 'OU=Childcare,OU=staff,OU=User'
  206.         $userinfo.Company = 'college'
  207.         $userinfo.Department = 'staff'
  208.         $userinfo.ScriptPath = 'DAYCARE.cmd'
  209.     }
  210.  
  211.     '^Library$' {
  212.         $userinfo.Path = 'OU=Library,OU=staff,OU=User'
  213.         $userinfo.Company = 'college'
  214.         $userinfo.Department = 'staff'
  215.         $userinfo.ScriptPath = 'LIBRARYSTAFF.cmd'
  216.     }
  217.  
  218.     '^NonTeaching$' {
  219.         $userinfo.Path = 'OU=NonTeaching,OU=staff,OU=User'
  220.         $userinfo.Company = 'college'
  221.         $userinfo.Department = 'staff'
  222.         $userinfo.ScriptPath = 'TEACHER.cmd'
  223.     }
  224.  
  225.     '^RBCChurch$' {
  226.         $userinfo.Path = 'OU=RBCChurch,OU=staff,OU=User'
  227.         $userinfo.Company = 'church'
  228.         $userinfo.Department = 'staff'
  229.         $userinfo.ScriptPath = 'church.cmd'
  230.     }
  231.  
  232.     '^Teachers$' {
  233.         $userinfo.Path = 'OU=Teachers,OU=staff,OU=User'
  234.         $userinfo.Company = 'college'
  235.         $userinfo.Department = 'staff'
  236.         $userinfo.ScriptPath = 'TEACHER.cmd'
  237.     }
  238.  
  239.     # Primary Student Variables depending on $UserOUSel
  240.     '^Year \d+$' {
  241.         $userinfo.Company = 'students'
  242.     }
  243.  
  244.     '^Year [1-6]$' {
  245.         $userinfo.Path = "OU=$_,OU=Primary,OU=students,OU=User"
  246.         $userinfo.Department = 'Primary'
  247.     }
  248.  
  249.     '^Year 1$' {
  250.         $userinfo.ScriptPath = 'primarystudents-year2.cmd'
  251.     }
  252.  
  253.     '^Year 2$' {
  254.         $userinfo.ScriptPath = 'primarystudents-year2.cmd'
  255.     }
  256.  
  257.     '^Year 3$' {
  258.         $userinfo.ScriptPath = 'primarystudents.cmd'
  259.     }
  260.  
  261.     '^Year 4$' {
  262.         $userinfo.ScriptPath = 'primarystudents.cmd'
  263.     }
  264.  
  265.     '^Year 5$' {
  266.         $userinfo.ScriptPath = 'primarystudents.cmd'
  267.     }
  268.  
  269.     '^Year 6$' {
  270.         $userinfo.Path = 'OU=Year 1,OU=Primary,OU=students,OU=User'
  271.         $userinfo.ScriptPath = 'primarystudents.cmd'
  272.     }
  273.  
  274.     # Secondary Student Variables depending on $UserOUSel
  275.     '^Year (?:[7-9]|1[0-2])$' {
  276.         $userinfo.Path = "OU=$_,OU=Secondary,OU=students,OU=User"
  277.         $userinfo.Department = 'secondary'
  278.         $userinfo.ScriptPath = 'students.cmd'
  279.     }
  280. }
  281.  
  282. <#######################################
  283.           4. Domain Selection
  284. ########################################>
  285.  
  286. $Domain = Out-Menu -Object 'contoso.com', 'google.com' -Header 'Domain selection'
  287.  
  288. # Create Email Variable
  289. $userinfo.EmailAddress = $userinfo.SAMAccountName + '@' + $Domain
  290.  
  291. <#######################################
  292.          5. User Creation
  293. ########################################>
  294.  
  295. New-ADUser @userinfo
  296.  
  297. # SMTP Proxy Address
  298. Set-ADUser -Identity $userinfo.SAMAccountName -Add @{Proxyaddresses = 'SMTP:' + $userinfo.SAMAccountName + '@' + $Domain}
Add Comment
Please, Sign In to add comment