CMUIntegrations/Get-PearsonMyLabPlusData.ps1

$ScriptName = $($MyInvocation.MyCommand.Name).Split(".")[0]
Start-Transcript -path "D:\logs\$ScriptName - $(Get-Date -Format 'yyyy-MM-dd-HH-mm-ss').txt"
$VerbosePreference = "continue"
$DebugPreference = "continue"
$ErrorActionPreference = "stop"
## This script gets active instructor and student enrollments for courses that use Pearson MyLabs Plus. And sends that to John Jackson for processing.
## It is working as of 01/12/2021.

#Import Useful Modules
Import-Module PSBlackboard -Verbose -force

#Script Variables
[System.Collections.ArrayList]$CourseMemberships = @()
[System.Collections.ArrayList]$FeedFileOutput = @()
[System.Collections.ArrayList]$Courses = @()

#Get Useful Terms
$CurrentTerms = Get-BBTerms -TargetTerm 'AllCurrent'
$LMSConnectDataSourceKey = Get-BBDataSources -ExternalID 'LMSConnect'

#Get the courses and filter to only the course designators we are lookingfor
foreach($Term in $CurrentTerms){
    Get-BBCourses -Filter "termId=$($Term.id)" -Verbose |
        Where-object {($_.courseId -like "MTH101*") -or ($_.courseId -like "MTH105*") -or ($_.courseId -like "MTH107*") -or ($_.courseId -like "MTH130*") -or ($_.courseId -like "STA282*") -or ($_.courseId -like "AST111*")} |
            ForEach-Object{
                $Courses.Add($_) | out-null
            }
        }
    #Get Available Instructor, Teaching Assistant, and Student memeberships. Then, only get those that are LMSConnect Enrollments. Add the records to be output later in the format that it needs.
$Counter = 0
Foreach($Course in $Courses) {
    $Counter += 1
    Write-Verbose "Processing Course Enrollments Progress: $($Counter)/$($Courses.count) ($($($Counter)/$($Courses.count))) [$($Course.id)] PercentComplete $(($($Counter)/$($Courses.count))*100)"
    $params = @{
        CourseID =  $Course.id
        ExpandUser = $true
    }
    Write-Verbose "Processing memberships for $($_.id)"
    Get-BBCourseMemberships @params -Verbose -Filter "role=Instructor&availability.available=Yes&datasourceId=$($LMSConnectDataSourceKey[0].id)" |
    foreach-Object {
        $CourseMemberships.Add($_) | out-null
    }

    Get-BBCourseMemberships @params -Filter "role=TeachingAssistant&availability.available=Yes&datasourceId=$($LMSConnectDataSourceKey[0].id)" |
    foreach-Object {
        $CourseMemberships.Add($_)| out-null
    }

    Get-BBCourseMemberships @params -Filter "role=Student&availability.available=Yes&datasourceId=$($LMSConnectDataSourceKey[0].id)" |
    foreach-Object {
        $CourseMemberships.Add($_)| out-null
    }
}

#Instructor/TA/Student Enrollments
ForEach($Membership in $CourseMemberships){
    if ($Membership.courseRoleId -eq 'Instructor'){
        $Role = '1'
    }
    if ($Membership.courseRoleId -eq 'TeachingAssistant'){
        $Role = '1'
    }
    if ($Membership.courseRoleId -eq 'Student'){
        $Role = '2'
    }
    $Course = $Courses | Where-Object {$_.id -eq $Membership.courseId}
    $Term = $Terms | Where-Object {$_.id -eq $Course.termId}
    $FeedFileOutput.Add($([PSCUstomObject]@{
        FirstName = $Membership.user.name.given
        MiddleName = ""
        LastName = $Membership.user.name.family
        UserId = $Membership.user.userName
        EmailAddress = $Membership.user.contact.email
        TermSeason = $Membership.externalID
        Blank1 = ""
        Blank2 = ""
        Blank3 = ""
        Blank4 = ""
        Blank5 = ""
        Blank6 = ""
        Blank7 = ""
        Blank8 = ""
        courseId = $Course.courseId
        Role = $Role
        1 = '1'
    })) | Out-Null
}

#Save the CSV that we need since you can't mail an attachment without it existing on the server. Sorted aphabetically, without header information and no quotes.

If ($CourseMemberships.count -eq 0){
    Write-verbose "No Records to Process"

    $subject = "No Records were found for the MyLabPlus data grab."
    $Body = "You'll want to take a look $($Terms) "
    Send-MailMessage -To 'jacks3m@cmich.edu' -Subject $Subject -Body $Body -SmtpServer 'smtp.cmich.edu' -From 'no-reply@blackboard.apps.cmich.edu'
}else{
    $FileName = "$PSScriptRoot\temp\PearsonMyLabPlusData.txt"
    $FeedFileOutput | Sort-Object -Property FirstName | ConvertTo-Csv  -NoTypeInformation | ForEach-Object { $_ -replace '"', ""} | select-object -Skip 1 |Set-Content -path $FileName

    #$recipients = "Marcus Jackson <jacks3m@cmich.edu>", "John Jackson <jacks1jl@cmich.edu>"
    $recipients = "Marcus Jackson <jacks3m@cmich.edu>"
    #Send the mail to whomever needs it.
    $subject = "Attached are the results of getting Pearson MyLab Plus data"
    $Body = "There are $($FeedFileOutput.count) records. Terms included: $($Terms). You'll want to upload this to https://cmich.mylabsplus.com/"
    Send-MailMessage -To $recipients -Subject $Subject -Body $Body -SmtpServer 'smtp.cmich.edu' -From 'no-reply@blackboard.apps.cmich.edu' -Attachment $FileName

    #Remove the CSV as it is no longer needed.
    remove-item $FileName
}