Parsing out URL’s within the physique of a Message with EWS and Powershell

Parsing out URL’s within the physique of a Message with EWS and Powershell Typically when your writing an Automation script you would possibly need to parse a sure URL from the Physique of a Message. One instance could be the Lync Assembly URL from an On-line Assembly invitation or one other would possibly however a DropBox URL for a shared file.

To seize the Physique of a Message within the EWS Managed API you’ll want to use both Load() or LoadPropertiesForItems() in case you have a lot of messages your processing. Utilizing these strategies will do a GetItem (or batch GetItem) in EWS.

To parse the URL’s from the HTML physique markup that EWS returns you should utilize some RegEx to separate out all of the hyperlinks. Then you should utilize the URI class from .internet to parse the matches additional and establish the hosts within the URL to see if its the URL your in search of. The next pattern will loop by the final 100 emails in a mailbox’s and parse any Lync Assembly URL’s (for Workplace365) or Dropbox URL’s.

I’ve put a download of this script right here

The code seems to be like

  1. ## Get the Mailbox to Entry from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4.   
  5. ## Load Managed API dll    
  6. Add-Kind -Path “C:Program FilesMicrosoftExchangeWeb Services2.0Microsoft.Exchange.WebServices.dll”    
  7.     
  8. ## Set Trade Model    
  9. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Trade2010_SP2    
  10.     
  11. ## Create Trade Service Object    
  12. $service = New-Object Microsoft.Trade.WebServices.Information.ExchangeService($ExchangeVersion)    
  13.     
  14. ## Set Credentials to use two choices are availible Option1 to use explict credentials or Possibility 2 use the Default (logged On) credentials    
  15.     
  16. #Credentials Possibility 1 utilizing UPN for the home windows Account    
  17. $psCred = Get-Credential    
  18. $creds = New-Object System.Internet.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  19. $service.Credentials = $creds        
  20.     
  21. #Credentials Possibility 2    
  22. #service.UseDefaultCredentials = $true    
  23.     
  24. ## Select to ignore any SSL Warning points brought about by Self Signed Certificates    
  25.     
  26. ## Code From http://poshcode.org/624  
  27. ## Create a compilation setting  
  28. $Supplier=New-Object Microsoft.CSharp.CSharpCodeProvider  
  29. $Compiler=$Supplier.CreateCompiler()  
  30. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  31. $Params.GenerateExecutable=$False  
  32. $Params.GenerateInMemory=$True  
  33. $Params.IncludeDebugInformation=$False  
  34. $Params.ReferencedAssemblies.Add(“System.DLL”) | Out-Null  
  35.   
  36. $TASource[email protected] 
  37.   namespace Native.ToolkitExtensions.Internet.CertificatePolicy{ 
  38.     public class TrustAll : System.Internet.ICertificatePolicy { 
  39.       public TrustAll() {  
  40.       } 
  41.       public bool CheckValidationResult(System.Internet.ServicePoint sp, 
  42.         System.Safety.Cryptography.X509Certificates.X509Certificates cert,  
  43.         System.Internet.WebRequest req, int downside) { 
  44.         return true; 
  45.       } 
  46.     } 
  47.   } 
  48. @   
  49. $TAResults=$Supplier.CompileAssemblyFromSource($Params,$TASource)  
  50. $TAAssembly=$TAResults.CompiledAssembly  
  51.   
  52. ## We now create an occasion of the TrustAll and connect it to the ServicePointManager  
  53. $TrustAll=$TAAssembly.CreateInstance(“Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll”)  
  54. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  55.   
  56. ## finish code from http://poshcode.org/624  
  57.     
  58. ## Set the URL of the CAS (Consumer Entry Server) to use two choices are availbe to use Autodiscover to discover the CAS URL or Hardcode the CAS to use    
  59.     
  60. #CAS URL Possibility 1 Autodiscover    
  61. $service.AutodiscoverUrl($MailboxName,{$true})    
  62. “Using CAS Server : “ + $Service.url     
  63.      
  64. #CAS URL Possibility 2 Hardcoded    
  65.     
  66. #$uri=[system.URI] “https://casservername/ews/exchange.asmx”    
  67. #$service.Url = $uri      
  68.     
  69. ## Non-compulsory part for Trade Impersonation    
  70. $psPropset= new-object Microsoft.Trade.WebServices.Information.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  71. # Bind to the Inbox Folder  
  72. $folderid= new-object Microsoft.Trade.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)     
  73. $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
  74.   
  75. #Outline ItemView to retrive simply 1000 Objects      
  76. $ivItemView =  New-Object Microsoft.Trade.WebServices.Information.ItemView(100)      
  77. $fiItems = $service.FindItems($Inbox.Id,$ivItemView)      
  78. [Void]$service.LoadPropertiesForItems($fiItems,$psPropset)    
  79. foreach($Merchandise in $fiItems.Objects){        
  80.     #Course of Merchandise    
  81.     “Processing : “ + $Merchandise.Topic  
  82.     $dupChk = @{}  
  83.     $RegExHtmlLinks = “<a href=`”(.*?)`“>”  
  84.     $matchedItems = [regex]::matches($Merchandise.Physique, $RegExHtmlLinks,[system.Text.RegularExpressions.RegexOptions]::Singleline)  
  85.     foreach($Match in $matchedItems){  
  86.         $SplitVal = $Match.Worth.Break up(‘”‘)  
  87.         if($SplitVal.Rely -gt 0){  
  88.             $ParsedURI=[system.URI]$SplitVal[1]  
  89.             if($ParsedURI.Host -eq “meet.lync.com“){ 
  90.                 if(!$dupChk.Accommodates($ParsedURI.AbsoluteUri)){ 
  91.                     Write-Host -ForegroundColor Inexperienced   “LyncURL     : ” + $ParsedURI.AbsoluteUri 
  92.                     $dupChk.add($ParsedURI.AbsoluteUri,0) 
  93.                 } 
  94.             }  
  95.             if($ParsedURI.Host -eq “www.dropbox.com“){ 
  96.                 if(!$dupChk.Accommodates($ParsedURI.AbsoluteUri)){ 
  97.                     Write-Host -ForegroundColor Blue “DropBox    : ” + $ParsedURI.AbsoluteUri  
  98.                     $dupChk.add($ParsedURI.AbsoluteUri,0)  
  99.                 }  
  100.             }    
  101.         }  
  102.     }  
  103. }      

Leave a Reply