examples/Get-UserDateLastAccessCourse.ps1

#Load PSBlackboard Module
Import-Module .\PSBlackboard.psm1 -Force -Verbose

#Create user List
$UserList = @('banja1m','jarra1ta','rutka1tr','marvi1el','taylo10m')
$Terms = Get-BBTerms -TargetTerm AllCurrent -Verbose
$LMSConnectDataSourceKey = Get-BBDataSources -ExternalID 'LMSConnect' -Verbose

#Establish used arrays
$FeedFileOutput = @()

#Get the blackboard user record
foreach ($User in $UserList) {
    $BBUser = Get-BBUsers -ExternalID $User -Verbose
    $UserMemeberships = Get-BBUserMemberships -UserID $BBUser.ID -ExpandCourse $true -Verbose

    foreach ($Term in $Terms) {
        #Generate output object
            $UserMemeberships | Where-Object {($_.course.dataSourceId -eq $LMSConnectDataSourceKey.id) -and ($_.course.TermId -eq $Term.id)} | ForEach-Object {
                $FeedFileOutput += [PSCUstomObject]@{
                    FirstName = $BBUser.name.given
                    MiddleName = ""
                    LastName = $BBUser.name.family
                    GID = $BBUser.externalId
                    StudentNumber = $BBUser.studentId
                    EmailAddress = $BBUser.contact.email
                    CourseId = $_.course.externalId
                    LastAccess = if ($_.lastAccessed -eq $null){
                                ""
                                }else{
                                    $_.lastAccessed.ToLocalTime()
                                }
                    CourseAvailability = $_.course.availability.available
                    EnrollmentAvailability = $_.availability.available
                    Role = $_.courseRoleId
                    Term = $Term.externalId
                    TermStartDate = $Term.availability.duration.start.ToLocalTime()

                }
            }
        }
    }
    #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\Get-UserDateLastAccessCourse.csv'
$FeedFileOutput | Sort-Object -Property FirstName | ConvertTo-Csv  -NoTypeInformation | % { $_ -replace '"', ""} |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 User Date Last Access Courses"
$Body = "There are $($FeedFileOutput.count) records. Terms included: $($Terms)."
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