examples/Update-BBFacultListServList.ps1


#Import Useful Modules
Import-Module .\PSBlackboard.psm1 -Verbose -force

#Script Variables
$Courses = @()
$CourseMemberships = @()
$Returns = @()

#Get Useful Terms
$CurrentTerms = Get-BBTerms -TargetTerm 'AllCurrent'
#$CurrentTerms = Get-BBTerms -TermID 'externalId:30700'

#Get the courses
foreach($Term in $CurrentTerms){
    $Courses += Get-BBCourses -Filter "termId=$($Term.id)" -Verbose
}

#Get Available Instructor and Teaching Assistant memeberships based upon those couses. This shows a progress bar for the executions.
$Courses | foreach-Object -Begin{
        $Counter = 0
    } -Process {
        $Counter += 1
        Write-Progress -Activity "Processing Course Enrollments" -Status "Progress: $($Counter)/$($Courses.count) ($($($Counter)/$($Courses.count))) [$($_.id)]" -PercentComplete (($($Counter)/$($Courses.count))*100)
        $params = @{
            CourseID =  $_.id
            ExpandUser = $true
        }
            Write-Verbose "Processing memberships for $($_.id)"
            $CourseMemberships += Get-BBCourseMemberships @params -Filter "role=Instructor&availability.available=Yes"
            $CourseMemberships += Get-BBCourseMemberships @params -Filter "role=TeachingAssistant&availability.available=Yes"

        } -End {
        }

#Determine all unique users
$UniqueUsers = $CourseMemberships | sort-object -Property {$_.user.userName} -Unique

#Add the columns we need to an array
$UniqueUsers | foreach-Object -Process {
    $Returns += [PSCUstomObject]@{
        FirstName = $_.user.name.given
        LastName = $_.user.name.family
        EmailAddress = $_.user.contact.email
    }
}

#Save the CSV that we need since you can't mail an attachment without it existing on the server.
$Returns | Export-Csv -path .\examples\temp\BBFacultListServeList.csv -NoTypeInformation

#Send the mail to whomever needs it.
$subject = "Attached are the results of getting instructors for this term"
$Body = "Please See Attached"
Send-MailMessage -To 'jacks3m@cmich.edu' -Subject $Subject -Body $Body -SmtpServer 'smtp.cmich.edu' -From 'no-reply@blackboard.apps.cmich.edu' -Attachment '.\examples\temp\BBFacultListServeList.csv'

#REmove the CSV as it is no longer needed.
remove-item '.\examples\temp\BBFacultListServeList.csv'