Advertisement
Guest User

Student Creation Script

a guest
Aug 28th, 2016
975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Make sure script is running in correct folder.
  3. #
  4. CD "path to script"
  5. ##################
  6. # Static Entries #
  7. ##################
  8. $ExchangeServer = "ExchangeServer.domain.com"
  9. $LyncServer = "LyncServer.domain.com"
  10. $Registrar = "LyncServer.domain.com"
  11. #$dialplan = "Registrar:LyncServer.domain.com"
  12. $intdomain = "domain.com"
  13. $sipdomain = "domain.com"
  14. $company = "Company/School Name"
  15. $mbdb = "ExchangeMailDatabaseName"
  16. $website = "www.domain.com"
  17. #$umpolicy = "Unified Messaging Policy Name"
  18. $state = "US State"
  19. $SpecChars = '!', '"', '£', '$', '%', '&', '^', '*', '(', ')', '@', '=', '+', '¬', '`', '\', '<', '>', '.', '?', '/', ':', ';', '#', '~', "'", '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', ' ', 'jr', 'III', 'II'
  20. $remspecchars = [string]::join('|', ($SpecChars | % {[regex]::escape($_)}))
  21. $today = Get-Date
  22. $expiredate = (Get-Date).AddDays(21)
  23. Clear-Content .\CreateStudent_newstudents.csv
  24. Clear-Content .\CreateStudent_dupcheck.csv
  25. Clear-Content .\CreateStudent_LOG_Duplicates.txt
  26. Clear-Content .\CreateStudent_LOG_Created.txt
  27. Clear-Content .\CreateStudent_LOG_NoClass.txt
  28. $expiredate
  29. #$ErrorActionPreference= 'silentlycontinue'
  30. $script:countexists = 0
  31. $script:countinvalid = 0
  32. $script:countnew = 0
  33. $script:countnoclass = 0
  34.  
  35.  
  36. Add-Content "CreateStudent_LOG_AlreadyExists.txt" "########################################################"
  37. Add-Content "CreateStudent_LOG_AlreadyExists.txt" "           New import started: $today"
  38. Add-Content "CreateStudent_LOG_AlreadyExists.txt" "########################################################"
  39. Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "########################################################"
  40. Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "           New import started: $today"
  41. Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "########################################################"
  42. Add-Content "CreateStudent_LOG_NoClass.txt" "########################################################"
  43. Add-Content "CreateStudent_LOG_NoClass.txt" "           New import started: $today"
  44. Add-Content "CreateStudent_LOG_NoClass.txt" "########################################################"
  45. Add-Content "CreateStudent_LOG_Created.txt" "########################################################"
  46. Add-Content "CreateStudent_LOG_Created.txt" "           New import started: $today"
  47. Add-Content "CreateStudent_LOG_Created.txt" "########################################################"
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. ##############################
  58. # Import session information #
  59. ##############################
  60.  
  61. $User = "domain\user"
  62. $Pass = "supersecretpassword" | ConvertTo-SecureString -asPlainText -Force
  63. $MyCredential=New-Object -TypeName System.Management.Automation.PSCredential($User,$Pass)
  64. $ExchSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://$ExchangeServer/powershell -Credential $MyCredential
  65. Import-PSSession $ExchSession
  66. $LyncSessionOptions = New-PSSessionOption -SkipCACheck:$true -SkipCNCheck:$true -SkipRevocationCheck:$true
  67. $LyncSession = New-PSSession -ConnectionURI https://$LyncServer/ocspowershell -Credential $MyCredential -SessionOption $LyncSessionOptions
  68. Import-PSSession $LyncSession
  69.  
  70.  
  71.  
  72.  
  73.  
  74. Function dataimport {
  75.     #######################
  76.     # Data Import section #
  77.     #######################
  78.     $fname = $_.fname2
  79.     #$fname = $fname.Substring(0,1).ToUpper()+$fname.Substring(1).ToLower()
  80.     $finit = $fname.Substring(0,1)
  81.     #$mname = $_.mname
  82.     #$minit = $mname.Substring(0,1)
  83.     $lname = $_.lname2
  84.     #$lname = $lname.Substring(0,1).ToUpper()+$lname.Substring(1).ToLower()
  85.     $linit = $lname.Substring(0,1)
  86.     $name = $_.name
  87.     $password = (convertto-securestring "GenericPassword" -asplaintext -force)
  88.    
  89.     # Remove unwanted characters
  90.     $lname = $lname -replace $remspecchars, ""
  91.     $fname = $fname -replace $remspecchars, ""
  92.  
  93.     $program = $_.program2
  94.     $sam = $_.sam
  95.     Switch ($program)
  96.       {
  97.         AFS
  98.         {
  99.             $wing = "D-Wing"
  100.             $room = "D50"
  101.             $script = "D50"
  102.         }
  103.         AMT
  104.         {
  105.             $wing = "Aviation"
  106.             $room = "AVStudents"
  107.             $script = "AVStudents"
  108.         }
  109.         AMT1
  110.         {
  111.             $wing = "Aviation"
  112.             $room = "AV-B10"
  113.             $script = "AV-B10"
  114.         }
  115.         AMT2
  116.         {
  117.             $wing = "Aviation"
  118.             $room = "AV-B30"
  119.             $script = "AV-B30"
  120.         }
  121.         AMT3
  122.         {
  123.             $wing = "Aviation"
  124.             $room = "AV-A20"
  125.             $script = "AV-A20"
  126.         }
  127.         AMT4
  128.         {
  129.             $wing = "Aviation"
  130.             $room = "AV-C30"
  131.             $script = "AV-C30"
  132.         }
  133.         "AST Seminole"
  134.         {
  135.             $wing = "Seminole"
  136.             $room = "SM-S60"
  137.             $script = "SM-S60"
  138.         }
  139.         "AST Shawnee"
  140.         {
  141.             $wing = "G-Wing"
  142.             $room = "G60"
  143.             $script = "G60"
  144.         }
  145.         AWT
  146.         {
  147.             $wing = "B-Wing"
  148.             $room = "B30"
  149.             $script = "B30"
  150.         }
  151.         CMT
  152.         {
  153.             $wing = "H-Wing"
  154.             $room = "H30"
  155.             $script = "H30"
  156.         }
  157.         COS
  158.         {
  159.             $wing = "Seminole"
  160.             $room = "SM-S40"
  161.             $script = "SM-S40"
  162.         }
  163.         CRT
  164.         {
  165.             $wing = "C-Wing"
  166.             $room = "C40"
  167.             $script = "C40"
  168.         }
  169.         DMP
  170.         {
  171.             $wing = "Seminole"
  172.             $room = "SM-S20"
  173.             $script = "SM-S20"
  174.         }
  175.         ECE
  176.         {
  177.             $wing = "L-Wing"
  178.             $room = "L30"
  179.             $script = "L30"
  180.         }
  181.         ECT
  182.         {
  183.             $wing = "F-Wing"
  184.             $room = "F10"
  185.             $script = "F10"
  186.         }
  187.         EMT
  188.         {
  189.             $wing = "E-Wing"
  190.             $room = "E70"
  191.             $script = "E70"
  192.         }
  193.         GD
  194.         {
  195.             $wing = "G-Wing"
  196.             $room = "G20"
  197.             $script = "G20"
  198.         }
  199.         HAR
  200.         {
  201.             $wing = "H-Wing"
  202.             $room = "H40"
  203.             $script = "H40"
  204.         }
  205.         "HCC Seminole"
  206.         {
  207.             $wing = "Seminole"
  208.             $room = "SM-S50"
  209.             $script = "SM-S50"
  210.         }
  211.         "HCC Shawnee"
  212.         {
  213.             $wing = "E-Wing"
  214.             $room = "E90"
  215.             $script = "E90"
  216.         }
  217.         "LPN"
  218.         {
  219.             $wing = "E-Wing"
  220.             $room = "E15"
  221.             $script = "E15"
  222.         }
  223.         "LPN eve"
  224.         {
  225.             $wing = "E-Wing"
  226.             $room = "E60"
  227.             $script = "E60"
  228.         }
  229.         CNT
  230.         {
  231.             $wing = "E-Wing"
  232.             $room = "E20"
  233.             $script = "E20"
  234.         }
  235.         OMT
  236.         {
  237.             $wing = "D-Wing"
  238.             $room = "D80"
  239.             $script = "D80"
  240.         }
  241.         PDT
  242.         {
  243.             $wing = "M-Wing"
  244.             $room = "M20"
  245.             $script = "M20"
  246.         }
  247.         PDT2
  248.         {
  249.             $wing = "M-Wing"
  250.             $room = "M30"
  251.             $script = "M30"
  252.         }
  253.         PEA
  254.         {
  255.             $wing = "D-Wing"
  256.             $room = "Engineering"
  257.             $script = "Engineering"
  258.         }
  259.         PM
  260.         {
  261.             $wing = "B-Wing"
  262.             $room = "PrecisionMachining"
  263.             $script = "PrecisionMachining"
  264.         }
  265.         SCA
  266.         {
  267.             $wing = "C-Wing"
  268.             $room = "C30"
  269.             $script = "C30"
  270.         }
  271.         default
  272.         {
  273.             $wing = "bork"
  274.             $room = "bork"
  275.         }
  276.       }
  277.     $ou = "OU=Users,OU=$room,OU=$Wing,OU=Students,DC=domain,DC=com"
  278.     $title = $program
  279.     $desc = $title + " Student"
  280.     $dept = $room
  281.     #$city = $_.city
  282.     #$zip = $_.zip
  283.     $pwd = $password
  284.     $upn = $sam + "@" + $intdomain
  285.     $alias = $sam
  286.     $HomeDirectory = "\\ServerName\Share\$wing\$room\$sam"
  287.     $scriptpath = $script + ".bat"
  288.  
  289.  
  290.  
  291.     $date = Get-Date -Format g
  292.     $UserCheck = Get-ADUser -Identity $sam -ErrorVariable uce -EA SilentlyContinue
  293.     IF ($uce){Write-Host "New User!"}
  294.     IF ($UserCheck.SamAccountName -contains $sam)
  295.         {
  296.         "User " + $upn + " already exists and added to log."
  297.         Add-Content "CreateStudent_LOG_AlreadyExists.txt" "$name already exists. Updating expiredate to $expiredate"
  298.         $script:countexists ++
  299.         updateuser
  300.         return
  301.         }
  302.     $pat = "^[a-zA-Z]+$"
  303.     IF ($lname -notmatch $pat )
  304.         {
  305.         "Error: Invalid Characters found in name: " + $upn + ". Added to log."
  306.         Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "$name has spacces or invalid characters"
  307.         $script:countinvalid ++
  308.         return
  309.         }
  310.     IF ($wing -eq "bork")
  311.         {
  312.         "User " + $upn + " has no class. Added to log. Class found is: " + $program
  313.         Add-Content "CreateStudent_LOG_NoClass.txt" "$name assigned to class: $program"
  314.         $script:countnoclass ++
  315.         return
  316.         }
  317.  
  318.         $script:countnew ++
  319.     createuser
  320. }
  321.  
  322. Function updateuser {
  323.     IF ($UserCheck.DistinguishedName -notcontains $ou)
  324.         {Get-ADUser -Identity $sam | Move-ADObject -TargetPath $ou}
  325.     ELSE {Write "User in correct OU"}
  326.     Enable-ADAccount -Identity $sam
  327.     Set-ADUser -Verbose `
  328.      -Identity $sam `
  329.      -Server ADServer.domain.com `
  330.      -AccountExpirationDate $expiredate `
  331.      -Enabled $true `
  332.      -Department $dept `
  333.      -Description $desc `
  334.      -HomeDirectory $HomeDirectory `
  335.      -Office $room `
  336.      -ScriptPath $scriptpath `
  337.      -Title $title
  338.      Add-ADGroupMember -Identity $room -Members $sam
  339.      Add-ADGroupMember -Identity Students -Members $sam
  340.  
  341. }
  342.  
  343. Function createuser {
  344.  
  345. "Creating " + $name + "."
  346. New-ADUser -Verbose `
  347.  -Server ADServer.domain.com `
  348.  -Name $name `
  349.  -Path $ou `
  350.  -SamAccountName  $sam `
  351.  -UserPrincipalName  $upn `
  352.  -AccountPassword (ConvertTo-SecureString "GenericPassword" -AsPlainText -Force) `
  353.  -ChangePasswordAtLogon $true  `
  354.  -Enabled $true `
  355.  -CannotChangePassword $false `
  356.  -Company  $company `
  357.  -Department  $dept `
  358.  -Description  $desc `
  359.  -DisplayName  $name `
  360.  -GivenName  $fname `
  361.  -HomeDirectory  $HomeDirectory `
  362.  -HomeDrive  "Z:" `
  363.  -HomePage  $website `
  364.  -HomePhone  $phone `
  365.  -Office  $room `
  366.  -OfficePhone  $phone `
  367.  -PasswordNeverExpires  $false `
  368.  -ScriptPath  $scriptpath `
  369.  -City  $city `
  370.  -State  $state `
  371.  -StreetAddress  $street `
  372.  -Surname  $lname `
  373.  -Title  $title `
  374.  -AccountExpirationDate $expiredate
  375.  #-Initials  $minit `
  376.  
  377. $date = Get-Date -Format g
  378. Add-Content "CreateStudent_LOG_Created.txt" `n$ou
  379. Add-Content "CreateStudent_LOG_Created.txt" "$name created as $sam."
  380. Add-Content "CreateStudent_newstudents.csv" "$fname,$lname,$program,$sam,$room"
  381. }
  382.  
  383. Function enablemailbox {
  384.  
  385. $fname = $_.fname
  386. $lname = $_.lname
  387. $name = $fname + " " + $lname
  388. $sam = $_.sam
  389. $room = $_.room
  390.  
  391.  Add-ADGroupMember -Identity $room -Members $sam
  392.  Add-ADGroupMember -Identity Students -Members $sam
  393. Enable-Mailbox -Database $mbdb -DomainController ADServer.domain.com -Identity $sam
  394.  
  395. }
  396.  
  397. Function enablelync {
  398.  
  399. $fname = $_.fname
  400. $lname = $_.lname
  401. $name = $fname + " " + $lname
  402. $sam = $_.sam
  403.  
  404. ##########################################
  405. # Enable for lync and configure settings #
  406. ##########################################
  407. "Enabling " + $fname + " " + $lname + " for Lync"
  408. #$lineuri = "tel:+1" + $phone + ";ext=" + $ext
  409. Get-mailbox -identity $sam | Enable-CsUser -DomainController ADServer.domain.com -RegistrarPool $Registrar -SipAddressType EmailAddress -SipDomain $sipdomain
  410.  
  411. ################################
  412. # Enable For Unified Messaging #
  413. ################################
  414. #"Configuring " + $fname + " " + $lname + " for Unified Messaging"
  415. #$sipuri = $fname + "." + $lname + "@" + $sipdomain
  416. #Get-Mailbox -identity $name | Enable-UMMailbox -PinExpired $true -UMMailboxPolicy $umpolicy -Extensions $ext -SIPResourceIdentifier $sipuri
  417. }
  418.  
  419. $countpath= "\\Server\Share\Folder\ps_students.csv"
  420. $importCSVVar=Import-Csv $countpath | Measure-Object | Select-Object
  421. $count=($importCSVVar).Count-1
  422. $count
  423. $countfull = $count
  424. $count = 1
  425.  
  426. $powerschoolimport = Import-Csv "\\Server\Share\Folder\ps_students.csv" -Header fname,lname,program,status,sid | sort sid
  427. $powerschoolimport | ForEach-Object {
  428.     Write-Host $count " of " $countfull
  429.     $fname = $_.fname
  430.     $lname = $_.lname
  431.     $program = $_.program
  432.     $status = $_.status
  433.     $sid = $_.sid
  434.     $name = $fname + " " + $lname
  435.     # Remove unwanted characters
  436.     $lname = $lname -replace $remspecchars, ""
  437.     $fname = $fname -replace $remspecchars, ""
  438.     $lname2 = $lname + "02"
  439.     $lname3 = $lname + "03"
  440.     $sam1 = $fname + "." + $lname
  441.     $sam2 = $fname + "." + $lname2
  442.     $sam3 = $fname + "." + $lname3
  443.     $dupfile = Import-Csv "CreateStudent_dupcheck.csv" -Header fname2,lname2,program2,sid2,sam,name
  444.     $dupcheck1 = $dupfile | where {$_.sam -eq $sam1}
  445.     $dupcheck2 = $dupfile | where {$_.sam -eq $sam2}
  446.     IF ($dupcheck2.sam -eq $sam2)
  447.         {
  448.         "DUPLICATE FOUND FOR " + $sam2 + "! Appending 03 to last name."
  449.         Add-Content "CreateStudent_LOG_Duplicates.txt" `n$date
  450.         Add-Content "CreateStudent_LOG_Duplicates.txt" "$name is a third duplicate."
  451.         Add-Content "CreateStudent_LOG_Duplicates.txt" "Adjusted Login: $sam3"
  452.         Add-Content "CreateStudent_LOG_Duplicates.txt" "Program: $program"
  453.         Add-Content "CreateStudent_dupcheck.csv" "$fname,$lname3,$program,$sid,$sam3,$name"
  454.         $sam3
  455.         }
  456.     ELSEIF ($dupcheck1.sam -eq $sam1)
  457.         {
  458.         "DUPLICATE FOUND FOR " + $sam + "! Appending 02 to last name."
  459.         Add-Content "CreateStudent_LOG_Duplicates.txt" `n$date
  460.         Add-Content "CreateStudent_LOG_Duplicates.txt" "$name is a duplicate."
  461.         Add-Content "CreateStudent_LOG_Duplicates.txt" "Adjusted Login: $sam2"
  462.         Add-Content "CreateStudent_LOG_Duplicates.txt" "Program: $program"
  463.         Add-Content "CreateStudent_dupcheck.csv" "$fname,$lname2,$program,$sid,$sam2,$name"
  464.         $sam2
  465.         }
  466.     ELSE
  467.         {
  468.         Add-Content "CreateStudent_dupcheck.csv" "$fname,$lname,$program,$sid,$sam1,$name"
  469.         $sam1
  470.         }
  471.    
  472.     $count ++
  473. Write-Host "------------------------------"
  474. }
  475.  
  476. $countpath= "CreateStudent_dupcheck.csv"
  477. $importCSVVar=Import-Csv $countpath | Measure-Object | Select-Object
  478. $count=($importCSVVar).Count-1
  479. $count
  480. $countfull = $count
  481. $count = 1
  482.  
  483. Import-Csv "CreateStudent_dupcheck.csv" -Header fname2,lname2,program2,sid2,sam,name | ForEach-Object {
  484. Write-Host $count " of " $countfull
  485. dataimport
  486. $count ++
  487. Write-Host "------------------------------"
  488. }
  489.  
  490. $countpath= "CreateStudent_newstudents.csv"
  491. $importCSVVar=Import-Csv $countpath | Measure-Object | Select-Object
  492. $count=($importCSVVar).Count-1
  493. $count
  494. $countfull = $count
  495. $count = 1
  496.  
  497. Import-Csv "CreateStudent_newstudents.csv" -Header fname,lname,program,sam,room | ForEach-Object {
  498.  
  499. enablemailbox
  500. Write-Host $count " of " $countfull
  501. $count ++
  502. Write-Host "------------------------------"
  503. }
  504.  
  505. $countpath= "CreateStudent_newstudents.csv"
  506. $importCSVVar=Import-Csv $countpath | Measure-Object | Select-Object
  507. $count=($importCSVVar).Count-1
  508. $count
  509. $countfull = $count
  510. $count = 1
  511.  
  512. Import-Csv "CreateStudent_newstudents.csv" -Header fname,lname,program,sam | ForEach-Object {
  513.  
  514. enablelync
  515. Write-Host $count " of " $countfull
  516. $count ++
  517. Write-Host "------------------------------"
  518. }
  519.  
  520. Write-Host "------------------------------"
  521. Write-Host $countexists " users updated"
  522. Write-Host "------------------------------"
  523. Write-Host $countinvalid " users with invalid characters"
  524. Write-Host "------------------------------"
  525. Write-Host $countnoclass " users have no class assigned"
  526. Write-Host "------------------------------"
  527. Write-Host $countnew " new users created"
  528. Write-Host "------------------------------"
  529.  
  530.  
  531. Add-Content "CreateStudent_LOG_AlreadyExists.txt" "########################################################"
  532. Add-Content "CreateStudent_LOG_AlreadyExists.txt" "$countexists users updated           Import Finished"
  533. Add-Content "CreateStudent_LOG_AlreadyExists.txt" "########################################################"
  534. Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "########################################################"
  535. Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "$countinvalid users with invalid characters           Import Finished"
  536. Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "########################################################"
  537. Add-Content "CreateStudent_LOG_NoClass.txt" "########################################################"
  538. Add-Content "CreateStudent_LOG_NoClass.txt" "$countnoclass users have no class assigned           Import Finished"
  539. Add-Content "CreateStudent_LOG_NoClass.txt" "########################################################"
  540. Add-Content "CreateStudent_LOG_Created.txt" "########################################################"
  541. Add-Content "CreateStudent_LOG_Created.txt" "$countnew new users created           Import Finished"
  542. Add-Content "CreateStudent_LOG_Created.txt" "########################################################"
  543.  
  544.  
  545.  
  546. IF ($countnew -gt 0)
  547.     {
  548.     $studentsnew = Get-Content -Path CreateStudent_LOG_Created.txt
  549.     $studentsnoclass = Get-Content -Path CreateStudent_LOG_NoClass.txt
  550.     Send-MailMessage -From UserAccountCreation<email@domain.com> `
  551.         -Subject "Student Creation Script" `
  552.         -To email@domain.com `
  553.         -Attachments CreateStudent_LOG_Created.txt `
  554.         -Body "List of new students created" `n `n `
  555.         "$studentsnew" `n `n
  556.         "$studentsnoclass"
  557.         -SmtpServer email.domain.com
  558.     }
  559.  
  560. IF ($countnoclass -gt 0)
  561.     {
  562.     Send-MailMessage -From UserAccountCreation<email@domain.com> `
  563.         -Subject "Student Creation Script" `
  564.         -To studentupdater@domain.com `
  565.         -Attachments CreateStudent_LOG_NoClass.txt `
  566.         -Body "List of new studends that need a program assigned in Powerschool under custom screens/demographics" `
  567.         -SmtpServer email.domain.com
  568.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement