LumifyTrainerTools.psm1

function Invoke-ClassTimer {
  <#
  .Synopsis
    Calculates the end of break times in multiple cities and shows a countdown timer
  .DESCRIPTION
    This program calclates event end times for Brisbane, Melbourne/Sydney, Adelaide
    Perth, Auckland and the USA Pacific TZ by default. Determining when each city
    should end the event they are undertaking break/lab, relevant to their timezone.
    You can enter the "Event Length" in minutes time and then click
    "Calculate End Time" and it will automatically determine when each city's end time
    should be. It will also start a countdown timer in color stages Green for > 2 min left,
    Orange for 0 >= time left >= 2, and Red for time is up!
  .EXAMPLE
    Invoke-ClassTimer
    This will start a gui tool to determine end of break times
  .NOTES
    General notes
      Created By: Brent Denny
      Created On: 28 May 2020
 
    Change Log:
      14Sep21: Changing the timer to be a general time and not just for breaks
               Cleand up the help and disabled Melbourne in the GUI
      24Aug22: Rewrote the entire command to be able to run form anywhere using UTC
               time values. also added color coding as the timer runs out, added
               each city as a combo box so that all of the cities can be changed.
      25Aug22: Modified the count down timer to refresh the window every second.
      12Dec22: Modified the colors of the countdown box and set the value back to
               actual minutes left.
      13Sep23 This has been completely re-written to show seconds as well as minutes
               the code is also streamlined and much easier to understand.
  #>


  [cmdletbinding()]
  Param ()

  $Script:TimeZones = Get-TimeZone -ListAvailable
  $UTCDateTime = [System.DateTime]::UtcNow
  
  function Get-FinishTime {
    Param (
      [System.TimeZoneInfo]$TimeZone,
      $fnUtcTime = [System.DateTime]::UtcNow,
      [Parameter(mandatory=$true)]
      [timespan]$Offset,
      [int]$fnSetTime = 0
    )
    if ($fnUtcTime -is [datetime]) {
      $DlsActive = $TimeZone.IsDaylightSavingTime([System.DateTimeOffset]::Now)
      $TimespanFromSettime = New-TimeSpan -Minutes $fnSetTime
      if ($DlsActive -eq $true) {$DLSAddHour = New-TimeSpan -Hours 1}
      else {$DLSAddHour = New-TimeSpan -Hours 0}
      $EndTimeObj = [datetime]($fnUtcTime + $Offset + $TimespanFromSettime + $DLSAddHour)
      $FormattedResult =   $EndTimeObj.ToShortTimeString() + ' ' + ($EndTimeObj.DayOfWeek).ToString()
      return [PSCustomObject]@{
        FormattedResult = $FormattedResult
        EndTimeObj      = $EndTimeObj
      }
    }
  }
    
  Add-Type -AssemblyName System.Windows.Forms
  [System.Windows.Forms.Application]::EnableVisualStyles()
  
  $formLumifyTimer                 = New-Object system.Windows.Forms.Form
  $formLumifyTimer.ClientSize      = New-Object System.Drawing.Point(578,708)
  $formLumifyTimer.text            = "Lumify Class Timer"
  $formLumifyTimer.TopMost         = $false
  
  $Groupbox1                       = New-Object system.Windows.Forms.Groupbox
  $Groupbox1.height                = 237
  $Groupbox1.width                 = 240
  $Groupbox1.location              = New-Object System.Drawing.Point(12,14)
  
  $Groupbox2                       = New-Object system.Windows.Forms.Groupbox
  $Groupbox2.height                = 234
  $Groupbox2.width                 = 277
  $Groupbox2.location              = New-Object System.Drawing.Point(267,14)
  
  $Groupbox3                       = New-Object system.Windows.Forms.Groupbox
  $Groupbox3.height                = 335
  $Groupbox3.width                 = 536
  $Groupbox3.location              = New-Object System.Drawing.Point(9,266)
  
  $butReset                        = New-Object system.Windows.Forms.Button
  $butReset.text                   = "Reset"
  $butReset.width                  = 60
  $butReset.height                 = 30
  $butReset.location               = New-Object System.Drawing.Point(389,633)
  $butReset.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',12)
  $butReset.Enabled                = $false
  
  $butClose                        = New-Object system.Windows.Forms.Button
  $butClose.text                   = "Close"
  $butClose.width                  = 60
  $butClose.height                 = 30
  $butClose.location               = New-Object System.Drawing.Point(476,633)
  $butClose.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',12)
  
  $labSetTime                      = New-Object system.Windows.Forms.Label
  $labSetTime.text                 = "Set Time"
  $labSetTime.AutoSize             = $true
  $labSetTime.width                = 25
  $labSetTime.height               = 10
  $labSetTime.location             = New-Object System.Drawing.Point(28,70)
  $labSetTime.Font                 = New-Object System.Drawing.Font('Microsoft Sans Serif',13)
  
  $tboxSetTime                     = New-Object system.Windows.Forms.TextBox
  $tboxSetTime.multiline           = $false
  $tboxSetTime.width               = 71
  $tboxSetTime.height              = 20
  $tboxSetTime.location            = New-Object System.Drawing.Point(115,63)
  $tboxSetTime.Font                = New-Object System.Drawing.Font('Microsoft Sans Serif',17)
  $tboxSetTime.Text                = 15
  
  $butStartTimer                   = New-Object system.Windows.Forms.Button
  $butStartTimer.text              = "Start Timer"
  $butStartTimer.width             = 100
  $butStartTimer.height            = 39
  $butStartTimer.location          = New-Object System.Drawing.Point(116,156)
  $butStartTimer.Font              = New-Object System.Drawing.Font('Microsoft Sans Serif',12)
  
  $Script:tboxTimeRemaining               = New-Object system.Windows.Forms.TextBox
  $Script:tboxTimeRemaining.multiline     = $false
  $Script:tboxTimeRemaining.width         = 190
  $Script:tboxTimeRemaining.height        = 20
  $Script:tboxTimeRemaining.location      = New-Object System.Drawing.Point(39,77)
  $Script:tboxTimeRemaining.Font          = New-Object System.Drawing.Font('Microsoft Sans Serif',50)
  $Script:tboxTimeRemaining.TextAlign     = 'Right'
  
  $LabTimeRemaining                = New-Object system.Windows.Forms.Label
  $LabTimeRemaining.text           = "Time Remaining"
  $LabTimeRemaining.AutoSize       = $true
  $LabTimeRemaining.width          = 25
  $LabTimeRemaining.height         = 10
  $LabTimeRemaining.location       = New-Object System.Drawing.Point(38,52)
  $LabTimeRemaining.Font           = New-Object System.Drawing.Font('Microsoft Sans Serif',12)
  
  $cboxTZ1                         = New-Object system.Windows.Forms.ComboBox
  $cboxTZ1.width                   = 311
  $cboxTZ1.height                  = 29
  $cboxTZ1.location                = New-Object System.Drawing.Point(37,46)
  $cboxTZ1.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',12)
  $cboxTZ1.Items.AddRange($Script:TimeZones)
  $cboxTZ1.DisplayMember           = 'DisplayName'
  $cboxTZ1.ValueMember             = 'Id'
  $cboxTZ1.SelectedItem            = $Script:TimeZones | Where-Object {$_.DisplayName -match 'Brisbane'}
  
  $cboxTZ2                         = New-Object system.Windows.Forms.ComboBox
  $cboxTZ2.width                   = 311
  $cboxTZ2.height                  = 29
  $cboxTZ2.location                = New-Object System.Drawing.Point(37,86)
  $cboxTZ2.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',12)
  $cboxTZ2.Items.AddRange($Script:TimeZones)
  $cboxTZ2.DisplayMember           = 'DisplayName'
  $cboxTZ2.ValueMember             = 'Id'
  $cboxTZ2.SelectedItem            = $Script:TimeZones | Where-Object {$_.DisplayName -match 'Sydney'}
  
  $cboxTZ3                         = New-Object system.Windows.Forms.ComboBox
  $cboxTZ3.width                   = 311
  $cboxTZ3.height                  = 29
  $cboxTZ3.location                = New-Object System.Drawing.Point(37,126)
  $cboxTZ3.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',12)
  $cboxTZ3.Items.AddRange($Script:TimeZones)
  $cboxTZ3.DisplayMember           = 'DisplayName'
  $cboxTZ3.ValueMember             = 'Id'
  $cboxTZ3.SelectedItem            = $Script:TimeZones | Where-Object {$_.DisplayName -match 'Adelaide'}
  
  $cboxTZ4                         = New-Object system.Windows.Forms.ComboBox
  $cboxTZ4.width                   = 311
  $cboxTZ4.height                  = 29
  $cboxTZ4.location                = New-Object System.Drawing.Point(37,166)
  $cboxTZ4.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',12)
  $cboxTZ4.Items.AddRange($Script:TimeZones)
  $cboxTZ4.DisplayMember           = 'DisplayName'
  $cboxTZ4.ValueMember             = 'Id'
  $cboxTZ4.SelectedItem            = $Script:TimeZones | Where-Object {$_.DisplayName -match 'Perth'}
  
  $cboxTZ5                         = New-Object system.Windows.Forms.ComboBox
  $cboxTZ5.width                   = 311
  $cboxTZ5.height                  = 29
  $cboxTZ5.location                = New-Object System.Drawing.Point(37,206)
  $cboxTZ5.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',12)
  $cboxTZ5.Items.AddRange($Script:TimeZones)
  $cboxTZ5.DisplayMember           = 'DisplayName'
  $cboxTZ5.ValueMember             = 'Id'
  $cboxTZ5.SelectedItem            = $Script:TimeZones | Where-Object {$_.DisplayName -match 'Tokyo'}
  
  $cboxTZ6                         = New-Object system.Windows.Forms.ComboBox
  $cboxTZ6.width                   = 311
  $cboxTZ6.height                  = 29
  $cboxTZ6.location                = New-Object System.Drawing.Point(37,246)
  $cboxTZ6.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',12)
  $cboxTZ6.Items.AddRange($Script:TimeZones)
  $cboxTZ6.DisplayMember           = 'DisplayName'
  $cboxTZ6.ValueMember             = 'Id'
  $cboxTZ6.SelectedItem            = $Script:TimeZones | Where-Object {$_.DisplayName -match 'Hawaii'}
  
  $labTZ1                          = New-Object system.Windows.Forms.Label
  $labTZ1.text                     = (Get-FinishTime -TimeZone  $cboxTZ1.SelectedItem -fnUtcTime $Script:UTCDateTime -Offset $cboxTZ1.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
  $labTZ1.AutoSize                 = $true
  $labTZ1.width                    = 25
  $labTZ1.height                   = 10
  $labTZ1.location                 = New-Object System.Drawing.Point(355,50)
  $labTZ1.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',11)
  $labTZ1.Visible                  = $false
  
  $labTZ2                          = New-Object system.Windows.Forms.Label
  $labTZ2.text                     = (Get-FinishTime -TimeZone  $cboxTZ2.SelectedItem -fnUtcTime $Script:UTCDateTime -Offset $cboxTZ2.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
  $labTZ2.AutoSize                 = $true
  $labTZ2.width                    = 25
  $labTZ2.height                   = 10
  $labTZ2.location                 = New-Object System.Drawing.Point(355,90)
  $labTZ2.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',11)
  $labTZ2.Visible                  = $false
  
  $labTZ3                          = New-Object system.Windows.Forms.Label
  $labTZ3.text                     = (Get-FinishTime -TimeZone  $cboxTZ3.SelectedItem -fnUtcTime $Script:UTCDateTime -Offset $cboxTZ3.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
  $labTZ3.AutoSize                 = $true
  $labTZ3.width                    = 25
  $labTZ3.height                   = 10
  $labTZ3.location                 = New-Object System.Drawing.Point(355,130)
  $labTZ3.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',11)
  $labTZ3.Visible                  = $false
  
  $labTZ4                          = New-Object system.Windows.Forms.Label
  $labTZ4.text                     = (Get-FinishTime -TimeZone  $cboxTZ4.SelectedItem -fnUtcTime $Script:UTCDateTime -Offset $cboxTZ4.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
  $labTZ4.AutoSize                 = $true
  $labTZ4.width                    = 25
  $labTZ4.height                   = 10
  $labTZ4.location                 = New-Object System.Drawing.Point(355,170)
  $labTZ4.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',11)
  $labTZ4.Visible                  = $false
  
  $labTZ5                          = New-Object system.Windows.Forms.Label
  $labTZ5.text                     = (Get-FinishTime -TimeZone  $cboxTZ5.SelectedItem -fnUtcTime $Script:UTCDateTime -Offset $cboxTZ5.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
  $labTZ5.AutoSize                 = $true
  $labTZ5.width                    = 25
  $labTZ5.height                   = 10
  $labTZ5.location                 = New-Object System.Drawing.Point(355,210)
  $labTZ5.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',11)
  $labTZ5.Visible                  = $false
  
  $labTZ6                          = New-Object system.Windows.Forms.Label
  $labTZ6.text                     = (Get-FinishTime -TimeZone  $cboxTZ6.SelectedItem -fnUtcTime $Script:UTCDateTime -Offset $cboxTZ6.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
  $labTZ6.AutoSize                 = $true
  $labTZ6.width                    = 25
  $labTZ6.height                   = 10
  $labTZ6.location                 = New-Object System.Drawing.Point(355,250)
  $labTZ6.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',11)
  $labTZ6.Visible                  = $false
  
  $formLumifyTimer.controls.AddRange(@($Groupbox1,$Groupbox2,$Groupbox3,$butReset,$butClose))
  $Groupbox1.controls.AddRange(@($labSetTime,$tboxSetTime,$butStartTimer))
  $Groupbox2.controls.AddRange(@($tboxTimeRemaining,$LabTimeRemaining))
  $Groupbox3.controls.AddRange(@($cboxTZ1,$cboxTZ6,$cboxTZ2,$cboxTZ3,$cboxTZ4,$cboxTZ5,$labTZ1,$labTZ2,$labTZ3,$labTZ4,$labTZ5,$labTZ6))
  
  $butStartTimer.Add_Click({
    $butStartTimer.Enabled = $false
    if ($Script:Timer) {
      $Script:Timer.Stop()
      $Script:Timer.Dispose()
    }
    $butReset.Enabled = $true
    $EndTime = (Get-FinishTime -TimeZone  $cboxTZ1.SelectedItem -Offset ([timespan]::New(0)) -fnSetTime ($tboxSetTime.Text -as [int])).EndTimeObj
    $labTZ1.text = (Get-FinishTime -TimeZone  $cboxTZ1.SelectedItem -Offset $cboxTZ1.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
    $labTZ2.text = (Get-FinishTime -TimeZone  $cboxTZ2.SelectedItem -Offset $cboxTZ2.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
    $labTZ3.text = (Get-FinishTime -TimeZone  $cboxTZ3.SelectedItem -Offset $cboxTZ3.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
    $labTZ4.text = (Get-FinishTime -TimeZone  $cboxTZ4.SelectedItem -Offset $cboxTZ4.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
    $labTZ5.text = (Get-FinishTime -TimeZone  $cboxTZ5.SelectedItem -Offset $cboxTZ5.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
    $labTZ6.text = (Get-FinishTime -TimeZone  $cboxTZ6.SelectedItem -Offset $cboxTZ6.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])).FormattedResult
    $labTZ1.Visible = $true
    $labTZ2.Visible = $true
    $labTZ3.Visible = $true
    $labTZ4.Visible = $true
    $labTZ5.Visible = $true
    $labTZ6.Visible = $true
    $cboxTZ1.Enabled = $false
    $cboxTZ2.Enabled = $false
    $cboxTZ3.Enabled = $false
    $cboxTZ4.Enabled = $false
    $cboxTZ5.Enabled = $false
    $cboxTZ6.Enabled = $false
    $Script:EndTime = (Get-Date).AddMinutes(($tboxSetTime.text -as [int]))
    $Script:Timer = New-Object System.Windows.Forms.Timer
    $Script:Timer.Interval = 1000
    $Script:Timer.Add_Tick({
      $Remaining = $Script:EndTime - (Get-Date)
      $TotalMinutes = [int]($Remaining.Hours * 60) +  $Remaining.Minutes
      if ($Remaining.Seconds -lt 10) {$Filler = '0'}
      else {$Filler = ''}
      if ($TotalMinutes -gt 2) {$Groupbox2.BackColor = 'Green'}
      elseif ($TotalMinutes -gt 1) {$Groupbox2.BackColor = 'Orange'}
      elseif ($TotalMinutes -ge 0 -and $Remaining.TotalSeconds -ge 0) {$Groupbox2.BackColor = 'Red'}
      else {
        $Script:Timer.stop()
        $Script:Timer.Dispose()
      }
      $DisplayedTimeRemaining = '' + $TotalMinutes + ':' + $Filler +$Remaining.Seconds
      $Script:tboxTimeRemaining.Text = $DisplayedTimeRemaining 
    })
    $Script:Timer.Start()
  })
  $butReset.Add_Click({
    $butStartTimer.Enabled = $true
    $butReset.Enabled = $false
    $labTZ1.text = ''
    $labTZ2.text = ''
    $labTZ3.text = ''
    $labTZ4.text = ''
    $labTZ5.text = ''
    $labTZ6.text = ''
    $labTZ1.Visible = $true
    $labTZ2.Visible = $true
    $labTZ3.Visible = $true
    $labTZ4.Visible = $true
    $labTZ5.Visible = $true
    $labTZ6.Visible = $true
    $tboxSetTime.Text = 15
    $tboxTimeRemaining.Text = ''
    $cboxTZ1.Enabled = $true
    $cboxTZ2.Enabled = $true
    $cboxTZ3.Enabled = $true
    $cboxTZ4.Enabled = $true
    $cboxTZ5.Enabled = $true
    $cboxTZ6.Enabled = $true
    $Groupbox2.BackColor = ''
    $Script:Timer.Dispose()
    $Script:Timer.Stop()
  })
  $butClose.Add_Click({
    $butStartTimer.Enabled = $true
    $butReset.Enabled = $false
    $labTZ1.text = ''
    $labTZ2.text = ''
    $labTZ3.text = ''
    $labTZ4.text = ''
    $labTZ5.text = ''
    $labTZ6.text = ''
    $labTZ1.Visible = $true
    $labTZ2.Visible = $true
    $labTZ3.Visible = $true
    $labTZ4.Visible = $true
    $labTZ5.Visible = $true
    $labTZ6.Visible = $true
    $tboxSetTime.Text = 15
    $tboxTimeRemaining.Text = ''
    $cboxTZ1.Enabled = $true
    $cboxTZ2.Enabled = $true
    $cboxTZ3.Enabled = $true
    $cboxTZ4.Enabled = $true
    $cboxTZ5.Enabled = $true
    $cboxTZ6.Enabled = $true
    $Script:Timer.Dispose()
    $Script:Timer.Stop()
    $Groupbox2.BackColor = ''
    $formLumifyTimer.Close()
    $formLumifyTimer.Dispose()
  })
  $cboxTZ1.Add_SelectedValueChanged({  
    $labTZ1.text = Get-FinishTime -TimeZone  $cboxTZ1.SelectedItem -Offset $cboxTZ1.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])
  })
  $cboxTZ2.Add_SelectedValueChanged({  
    $labTZ2.text = Get-FinishTime -TimeZone  $cboxTZ2.SelectedItem -Offset $cboxTZ2.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])
  })
  $cboxTZ3.Add_SelectedValueChanged({  
    $labTZ3.text = Get-FinishTime -TimeZone  $cboxTZ3.SelectedItem -Offset $cboxTZ3.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])
  })
  $cboxTZ4.Add_SelectedValueChanged({  
    $labTZ4.text = Get-FinishTime -TimeZone  $cboxTZ4.SelectedItem -Offset $cboxTZ4.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])
  })
  $cboxTZ5.Add_SelectedValueChanged({  
    $labTZ5.text = Get-FinishTime -TimeZone  $cboxTZ5.SelectedItem -Offset $cboxTZ5.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])
  })
  $cboxTZ6.Add_SelectedValueChanged({  
    $labTZ6.text = Get-FinishTime -TimeZone  $cboxTZ6.SelectedItem -Offset $cboxTZ6.SelectedItem.BaseUtcOffset -fnSetTime ($tboxSetTime.Text -as [int])
  })
  
  #region Logic
  
  #endregion
  
  [void]$formLumifyTimer.ShowDialog()
}

function New-LumifyClassList {
    <#
    .Synopsis
      Lumify classlist exported from the Instructor Portal show as html
    .DESCRIPTION
      This command will take a CSV file exported from the Instructor Portal
      and convert it into a HTML document and launch the browser or it will
      show the information on screen in a table.
      The Csv data is obtained by going to the "Trainer Portal" in MS Teams,
      left-click within the area where the student info is found, choose the
      ellipsis (More Options) and then choose "Export Data" than
      "Summarized Data" and finally choose the file format as CSV and Click
      the Export button.
    .EXAMPLE
      New-LumifyClassList -CourseTitle 'AZ104'
      This will look for the data.csv file (by default) in the downloads folder of the
      currently loggedon user, it will then convert it to a timestamped csv filename
      and will create a timstamped html document that will show in Chrome browser
      that can be easily printed to show who is on the course. These files are created
      in the same directory that the original csv was found in.
    .EXAMPLE
      New-LumifyClassList -ExportedFilePath e:\data1.csv -NoHtml -CourseTitle 'AZ104'
      This will look for a data1.csv file in the e:\ drive and will then
      show the details on screen.
    .PARAMETER ExportedFilePath
      This is the path to the exported csv file. This includes that path and the
      filename of the CSV exported file.
    .PARAMETER CourseTitle
      The course title will be presented above the HTML table, it is a mandatory
      parameter
    .PARAMETER NoHtml
      This will prevent this command creating a HTML document but will instead
      display the details about the attendees in a table on the screen. More
      information is shown on the screen than would be shown as the html output.
    .PARAMETER ShowPersonalDetails
      This will show the basic information and the email address and phone number
      for each student. Without this parameter the phone number and email address
      will not be shown.
    .INPUTS
      String CSV
    .OUTPUTS
      file HTML
    .NOTES
      General notes
        Created by: Brent Denny
       
      Change Notes
        Version When What
        ------- ---- ----
        1.0 15-May-2023 finished the script initial code
        2.0 06-Jun-2023 Changed to height and alignment of the <TR> so that 12 students fit on one page
        3.0 06-Jul-2023 Saves the original file with a timestamp filename, opens the browser if using html
        4.0 05-Sep-2023 Rewritten to make the code simpler to read and understand also added updates to help
        5.0 08-Sep-2023 Changed the output to have a course title and sorting of students by Location and then name
                                    If nohtml is used it does not rename the file, and if original data file not found it searches
                                    for the last one that was accessed.
  #>

  [CmdletBinding(DefaultParameterSetName='Html')]
  Param(
    [string]$ExportedFilePath = $env:HOMEDRIVE + $env:HOMEPATH + '\downloads\data.csv',
    [Parameter(Mandatory=$true)]
    [string]$CourseTitle,
    [Parameter(ParameterSetName = 'Html')]
    [switch]$ShowPersonalDetails,
    [Parameter(ParameterSetName = 'NoHtml')]
    [switch]$NoHtml
  )
  # Check to see if the exported file exisits
  if (-not (Test-Path -Path $ExportedFilePath)) {
    $ExportedFileDir = ($ExportedFilePath | Split-Path).TrimEnd('\')
    $PreviousDataFiles = Get-ChildItem -Path $ExportedFileDir | Where-Object {$_.Name -like 'StudentList-*.csv'} | Sort-Object -Property LastAccessTime -Descending
    $ExportedFilePath = ($PreviousDataFiles | Select-Object -First 1 ).FullName
  }
  if (Test-Path -Path $ExportedFilePath){
    # Check to see if the Exported file can be imported
    if ($ExportedFilePath -match 'csv$') {
      $Css = '<style>h1 {text-align:center} table {border:1px solid black;border-collapse:collapse;width:100%;} th {border:1px solid black;border-collapse:collapse;text-align:center;} tr {text-align:left;vertical-align:top;border:1px solid black;border-collapse:collapse;height:75px;vertical-align:top;} tr:first-child {vertical-align:left;height:40px;}</style>'
      $ExportedFileDir = ($ExportedFilePath | Split-Path).TrimEnd('\')
      $NewHtmlFilePath = $ExportedFileDir + '\' + (Get-Date -UFormat 'StudentList-%Y%m%d%H%M%S.html')
      $NewCsvFilePath  = $ExportedFileDir + '\' + (Get-Date -UFormat 'StudentList-%Y%m%d%H%M%S.csv')
      try {
        if ($NoHtml -eq $false) {
          Rename-Item -Path $ExportedFilePath -NewName $NewCsvFilePath -Force -ErrorAction Stop
        }
        else {
          $NewCsvFilePath = $ExportedFilePath
        }
      }
      catch {Write-Warning "$ExportedFilePath was not able to be renamed"; break}
      $ExportedDataObj =  Get-Content -Path $NewCsvFilePath | 
                          ConvertFrom-Csv -Header 'StudentName','Company','Phone','Email','TrainingSite','State','Code1','Code2','LabCode','LabPassword','AccountManager' |
                          Select-Object -Skip 1 -Property 'StudentName',
                                                          'Company',
                                                          'Phone',
                                                          'Email',
                                                          'TrainingSite',
                                                          'State',
                                                          'Code1',
                                                          'Code2',
                                                          'LabCode',
                                                          'LabPassword',
                                                          'AccountManager' | 
                          Sort-Object -Property TrainingSite,StudentName
      if ($ShowPersonalDetails -eq $true) {$StudentInfo = $ExportedDataObj  | Select-Object -Property StudentName,Company,TrainingSite,Email}
      else {$StudentInfo = $ExportedDataObj  | Select-Object -Property StudentName,Company,TrainingSite,AccountManager,State}
      if ($NoHtml) {
        Clear-Host
        $ExportedDataObj | Select-Object -Property StudentName,Company,TrainingSite,Email,Phone,AccountManager  | Format-Table
      }
      else {
        try { $StudentInfo | ConvertTo-Html -Head $Css -PreContent "<h1>Course Title: $CourseTitle</h1>" | Out-File -FilePath $NewHtmlFilePath -ErrorAction Stop } 
        catch {Write-Warning "Could not save $NewHtmlFilePath file "; break}
        & "C:\Program Files\Google\Chrome\Application\chrome.exe" $NewHtmlFilePath          
      }
    }
    else {
      Write-Warning "$ExportedFilePath is not in a CSV format please re-export the file from the Teams Instructor Portal again" 
      break
    }
  }
  else {
    Write-Warning "$ExportedFilePath is not found"
    break
  }
}