Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. Import-module activedirectory
  2. Import-Module MSOnline
  3.  
  4. $Username = "admin@yourdomain.edu"
  5. $Password = ConvertTo-SecureString ‘YourPassword’ -AsPlainText -Force
  6. $LiveCred = New-Object System.Management.Automation.PSCredential $Username, $Password
  7. $senderemail="noreply@demo.com"
  8. $subject="Your Direct Reports and Permission"
  9.  
  10. $managerusers = Get-ADGroupMember -identity "Test_Department_Manager" | Select-Object SamAccountName, EmailAddress
  11.  
  12. Foreach ($manager in $managerusers)
  13. {
  14. # Find all direct user reporting to manager
  15. $directreportusers = Get-ADuser $manager -property directreports | select SamAccountName
  16. $manageremail = (Get-ADUser $manager -Properties Mail).Mail
  17.  
  18. Foreach ($user in $directreportusers)
  19. {
  20.  
  21. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
  22. Import-PSSession $Session
  23.  
  24. $email=$user.Mail
  25.  
  26. $permission=Get-MailboxPermission -Identity $email
  27.  
  28.  
  29. $cred = $LiveCred
  30. $body = $permission
  31. Send-MailMessage -To $manageremail -from $senderemail -Subject $subject -Body $body -BodyAsHtml -smtpserver smtp.office365.com -usessl -Credential $cred -Port 587
  32.  
  33. Remove-PSSession $Session
  34.  
  35. }
  36.  
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement