gangstanthony

Untitled

Mar 19th, 2022 (edited)
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. # https://www.reddit.com/r/PowerShell/comments/ti6ukx/weird_issue_with_no_solution/
  2. # must have impersonation access
  3. # https://cloudsolutions.atlassian.net/wiki/spaces/CKB/pages/4358166/Setting+up+Application+Impersonation+for+Exchange+or+Office+365#:~:text=Login%20to%20the%20Office%20365,Name%20and%20Description%20'CloudMigratorImpersonation'.&text=Click%20'Save'%20in%20the%20',role%20listed%20in%20Admin%20Roles.
  4.  
  5. $user = '<adminemail>'
  6. $pass = '<adminpass>'
  7.  
  8. $mailboxtoimpersonate = '<[email protected]>'
  9.  
  10. Add-Type -Path 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'
  11. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1
  12. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
  13. $service.Credentials = New-Object System.Net.NetworkCredential($user, $pass)
  14. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
  15. $Compiler=$Provider.CreateCompiler()
  16. $Params=New-Object System.CodeDom.Compiler.CompilerParameters
  17. $Params.GenerateExecutable=$False
  18. $Params.GenerateInMemory=$True
  19. $Params.IncludeDebugInformation=$False
  20. $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
  21. $TASource=@'
  22. namespace Local.ToolkitExtensions.Net.CertificatePolicy{
  23. public class TrustAll : System.Net.ICertificatePolicy {
  24. public TrustAll() {
  25. }
  26. public bool CheckValidationResult(System.Net.ServicePoint sp,
  27. System.Security.Cryptography.X509Certificates.X509Certificate cert,
  28. System.Net.WebRequest req, int problem) {
  29. return true;
  30. }
  31. }
  32. }
  33. '@
  34. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
  35. $TAAssembly=$TAResults.CompiledAssembly
  36. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
  37. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
  38. $uri=[system.URI] 'https://outlook.office365.com/ews/exchange.asmx'
  39. $service.Url = $uri
  40.  
  41. # Get last email from mailbox
  42. $service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $mailboxtoimpersonate)
  43. $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
  44. $mails = $inbox.FindItems(10000)
  45.  
  46. # really only need to load however many emails you want to check
  47. #$mails | select -f 5 | % {$_.Load()}
  48. $mails | % {$_.Load()}
  49.  
  50. # testing
  51. #$from = $mails.Items[0].Sender.address
  52. #$date = $mails.Items[0].DateTimeReceived.ToString()
  53. #$message = '' | select @{n='from';e={$from}}, @{n='date';e={$date}}
  54.  
  55. # body of most recent email
  56. $mails.items[0].body.text
Add Comment
Please, Sign In to add comment