examples/Get-PearsonMyLabPlusData.ps1

## 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.psm1 -Verbose -force

#Script Variables
$BlackboardOutput = @()
$FeedFileOutput = @()

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

#Get the courses and filter to only the course designators we are lookingfor
foreach($Term in $CurrentTerms){
    $CoursesToLookFor = 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*")}
    #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.
    if ($CoursesToLookFor.count -gt 0){
    $CoursesToLookFor | foreach-Object -Begin{

            $Students = @()
            $Instructors = @()
            $Counter = 0
        } -Process {
            $Counter += 1
            Write-Verbose "Processing Course Enrollments Progress: $($Counter)/$($_.count) ($($($Counter)/$($_.count))) [$($_.id)] PercentComplete $(($($Counter)/$($_.count))*100)"
            $params = @{
                CourseID =  $_.id
                ExpandUser = $true
            }
                Write-Verbose "Processing memberships for $($_.id)"
                $Instructors += Get-BBCourseMemberships @params -Filter "role=Instructor&availability.available=Yes&datasourceId=_175_1" -verbose
                $Instructors += Get-BBCourseMemberships @params -Filter "role=TeachingAssistant&availability.available=Yes&datasourceId=_175_1" -verbose
                $Students += Get-BBCourseMemberships @params -Filter "role=Student&availability.available=Yes&datasourceId=_175_1" -verbose
            } -End {
                $BlackboardOutput += Join-Object -left $Instructors -right $CoursesToLookFor -LeftJoinProperty 'courseId' -RightJoinProperty 'id' -Prefix 'course_'
                $BlackboardOutput += Join-Object -left $Students -right $CoursesToLookFor -LeftJoinProperty 'courseId' -RightJoinProperty 'id' -Prefix 'course_'
            }
        }
}


If ($BlackboardOutput.count -gt 0){
#Add Term Inforamtion
$BlackboardOutput = Join-Object -left $BlackboardOutput -Right $CurrentTerms -LeftJoinProperty 'course_termId' -RightJoinProperty 'id' -Prefix 'term_'

$FeedFileOutput = @()
#Instructor/TA/Student Enrollments
$BlackboardOutput | ForEach-Object {
    if ($_.courseRoleId -eq 'Instructor'){
        $Role = '1'
    }
    if ($_.courseRoleId -eq 'TeachingAssistant'){
        $Role = '1'
    }
    if ($_.courseRoleId -eq 'Student'){
        $Role = '2'
    }

    $FeedFileOutput += [PSCUstomObject]@{
        FirstName = $_.user.name.given
        MiddleName = ""
        LastName = $_.user.name.family
        UserId = $_.user.userName
        EmailAddress = $_.user.contact.email
        TermSeason = $_.term_externalId
        Blank1 = ""
        Blank2 = ""
        Blank3 = ""
        Blank4 = ""
        Blank5 = ""
        Blank6 = ""
        Blank7 = ""
        Blank8 = ""
        courseId = $_.'course_courseId'
        Role = $Role
        1 = '1'
    }
}

#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.
$FileName = '.\examples\temp\PearsonMyLabPlusData.txt'
$FeedFileOutput | Sort-Object -Property FirstName | ConvertTo-Csv  -NoTypeInformation | % { $_ -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
}else {
    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'

}