Advertisement
Guest User

Untitled

a guest
May 4th, 2018
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  Function New-O365ExchangeSession(){
  2.  
  3.     param(
  4.     [parameter(mandatory=$true)]
  5.     $Credentials)
  6.  
  7.     #close any old remote session
  8.     Get-PSSession | Remove-PSSession -Confirm:$false
  9.     [System.GC]::Collect()
  10.  
  11.     #start a new office 365 remote session
  12.     $365session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credentials -Authentication Basic -AllowRedirection
  13.     Import-PSSession $365session -AllowClobber
  14. }
  15.  
  16. $startd = Get-Date
  17.  
  18. ## Get Password
  19. ## Removed credential retrieval for pastebin
  20.  
  21. ## Grab the middle school students and list of classes students take
  22. $schools = Import-Csv C:\scripts\Dirsync\SDS\middleschools.csv
  23. $students = Import-Csv C:\scripts\Dirsync\SDS\student.csv | Where-Object { $schools.sisid -contains $_."school SIS ID" }
  24. $stuenroll = Import-Csv C:\scripts\Dirsync\SDS\StudentEnrollment.csv
  25.  
  26. ## Connect to Exchange Online
  27. New-O365ExchangeSession -Credentials $cred
  28.  
  29. ## Get all O365 groups for MS Classroom
  30. $Groups = Get-UnifiedGroup -ResultSize Unlimited -Filter {(name -like "Section_*") -and (alias -notlike "SDSRetired*")}
  31.  
  32. ## Get count and start breaking up the list to keep O365 session from timing out
  33. $rescount = $students.Count
  34. $offset = 0
  35. $pagesize = 200
  36.  
  37. Do {
  38.  
  39.     $students | Select-Object -Skip $offset -First $pagesize | ForEach-Object {
  40.         ## Set variable for ease of use
  41.         $s = $_
  42.  
  43.         ## Get Mailbox for each entry
  44.         $GMBX = Get-Mailbox $s.username
  45.         Write-Host $s.username
  46.  
  47.         ## Query for Custom Attribute 2
  48.         $restricted = ($GMBX).customattribute2
  49.         $restricted
  50.  
  51.         ## If CA2 is set to "Restricted" we will add group to the acceptance restrictions on the mailbox
  52.         If ($restricted -eq "Restricted") {
  53.             ## Get the classes the student is enrolled in
  54.             $classes = $stuenroll | ? { $_."sis id" -contains $s."sis id"}
  55.                  
  56.             foreach ($c in $classes){
  57.                 ## Find the correct O365 group for that class, set it to the student restrictions if not already set
  58.                 $assign = $groups | ? {$_.name.replace("Section_","") -like "*$($c.'Section SIS ID')*"}
  59.  
  60.                 ## Set $accept as an explicit string of AcceptMessagesOnlyFromSendersOrMembers to fix if statement
  61.                 [string]$accept = $gmbx.AcceptMessagesOnlyFromSendersOrMembers
  62.                 if ($accept -notlike "*$($c.'Section SIS ID')*"){
  63.                     Set-Mailbox $s.Username -AcceptMessagesOnlyFromSendersorMembers @{add= $assign.Name}
  64.                     Write-Host "set mailbox on " $s.username
  65.                     $assign.name
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     ##Recreate the session to keep it from timing out
  71.     New-O365ExchangeSession -Credentials $cred
  72.  
  73.     ## Set offset to restart at next 200
  74.     $Offset += $PageSize
  75. }
  76. While ($offset -lt $rescount)
  77.  
  78. $startd
  79. get-date
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement