Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function New-O365ExchangeSession(){
- param(
- [parameter(mandatory=$true)]
- $Credentials)
- #close any old remote session
- Get-PSSession | Remove-PSSession -Confirm:$false
- [System.GC]::Collect()
- #start a new office 365 remote session
- $365session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credentials -Authentication Basic -AllowRedirection
- Import-PSSession $365session -AllowClobber
- }
- $startd = Get-Date
- ## Get Password
- ## Removed credential retrieval for pastebin
- ## Grab the middle school students and list of classes students take
- $schools = Import-Csv C:\scripts\Dirsync\SDS\middleschools.csv
- $students = Import-Csv C:\scripts\Dirsync\SDS\student.csv | Where-Object { $schools.sisid -contains $_."school SIS ID" }
- $stuenroll = Import-Csv C:\scripts\Dirsync\SDS\StudentEnrollment.csv
- ## Connect to Exchange Online
- New-O365ExchangeSession -Credentials $cred
- ## Get all O365 groups for MS Classroom
- $Groups = Get-UnifiedGroup -ResultSize Unlimited -Filter {(name -like "Section_*") -and (alias -notlike "SDSRetired*")}
- ## Get count and start breaking up the list to keep O365 session from timing out
- $rescount = $students.Count
- $offset = 0
- $pagesize = 200
- Do {
- $students | Select-Object -Skip $offset -First $pagesize | ForEach-Object {
- ## Set variable for ease of use
- $s = $_
- ## Get Mailbox for each entry
- $GMBX = Get-Mailbox $s.username
- Write-Host $s.username
- ## Query for Custom Attribute 2
- $restricted = ($GMBX).customattribute2
- $restricted
- ## If CA2 is set to "Restricted" we will add group to the acceptance restrictions on the mailbox
- If ($restricted -eq "Restricted") {
- ## Get the classes the student is enrolled in
- $classes = $stuenroll | ? { $_."sis id" -contains $s."sis id"}
- foreach ($c in $classes){
- ## Find the correct O365 group for that class, set it to the student restrictions if not already set
- $assign = $groups | ? {$_.name.replace("Section_","") -like "*$($c.'Section SIS ID')*"}
- ## Set $accept as an explicit string of AcceptMessagesOnlyFromSendersOrMembers to fix if statement
- [string]$accept = $gmbx.AcceptMessagesOnlyFromSendersOrMembers
- if ($accept -notlike "*$($c.'Section SIS ID')*"){
- Set-Mailbox $s.Username -AcceptMessagesOnlyFromSendersorMembers @{add= $assign.Name}
- Write-Host "set mailbox on " $s.username
- $assign.name
- }
- }
- }
- }
- ##Recreate the session to keep it from timing out
- New-O365ExchangeSession -Credentials $cred
- ## Set offset to restart at next 200
- $Offset += $PageSize
- }
- While ($offset -lt $rescount)
- $startd
- get-date
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement