by

Create a Public Folder (or Mailbox folder) Put up utilizing EWS and Powershell

Create a Public Folder (or Mailbox folder) Put up utilizing EWS and Powershell Anyone requested a few weeks in the past about creating Put up Gadgets utilizing EWS and Powershell which I have never posted a pattern for beforehand. Given its the primary day of the yr an instance of a put up about find out how to create a put up to want all people glad new yr looks like good thought.

Making a POST merchandise utilizing the EWS Managed API is fairly straight ahead as their is a PostItem Class you should use. To create a put up is a folder you simply have to know the ewsID of the folder you wish to create the POST in. So in EWS you want some code that may both search and discover the Folder within the Mailbox you wish to put up to or the Public Folder you wish to put up to.  I’ve created two pattern one present find out how to create a put up in Mailbox folders and the opposite in a Public folder. I’ve posted a download of the 2 pattern right here . The code for making a POST in a Public folder 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.Knowledge.ExchangeService($ExchangeVersion)    
  13.     
  14. ## Set Credentials to use two choices are availible Option1 to use explict credentials or Choice 2 use the Default (logged On) credentials    
  15.     
  16. #Credentials Choice 1 utilizing UPN for the home windows Account    
  17. $psCred = Get-Credential    
  18. $creds = New-Object System.Web.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  19. $service.Credentials = $creds        
  20.     
  21. #Credentials Choice 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=@ 
  37.   namespace Native.ToolkitExtensions.Web.CertificatePolicy{ 
  38.     public class TrustAll : System.Web.ICertificatePolicy { 
  39.       public TrustAll() {  
  40.       } 
  41.       public bool CheckValidationResult(System.Web.ServicePoint sp, 
  42.         System.Safety.Cryptography.X509Certificates.X509Certificates cert,  
  43.         System.Web.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 Choice 1 Autodiscover    
  61. $service.AutodiscoverUrl($MailboxName,{$true})    
  62. “Using CAS Server : “ + $Service.url     
  63.      
  64. #CAS URL Choice 2 Hardcoded    
  65.     
  66. #$uri=[system.URI] “https://casservername/ews/exchange.asmx”    
  67. #$service.Url = $uri      
  68.     
  69. ## Non-obligatory part for Trade Impersonation    
  70.     
  71. #$service.ImpersonatedUserId = new-object Microsoft.Trade.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  72.   
  73. operate FolderIdFromPath{  
  74.     param (  
  75.             $FolderPath = “$( throw ‘Folder Path is a mandatory Parameter’ )”  
  76.           )  
  77.     course of{  
  78.         ## Discover and Bind to Folder based mostly on Path    
  79.         #Outline the path to search ought to be seperated with     
  80.         #Bind to the MSGFolder Root    
  81.         $folderid = new-object Microsoft.Trade.WebServices.Knowledge.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot)     
  82.         $tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)    
  83.         #Cut up the Search path into an array    
  84.         $fldArray = $FolderPath.Cut up(“”)  
  85.          #Loop via the Cut up Array and do a Search for every stage of folder  
  86.         for ($lint = 1; $lint -lt $fldArray.Size; $lint++) {  
  87.             #Carry out search based mostly on the displayname of every folder stage  
  88.             $fvFolderView = new-object Microsoft.Trade.WebServices.Knowledge.FolderView(1)  
  89.             $SfSearchFilter = new-object Microsoft.Trade.WebServices.Knowledge.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$fldArray[$lint])  
  90.             $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)  
  91.             if ($findFolderResults.TotalCount -gt 0){  
  92.                 foreach($folder in $findFolderResults.Folders){  
  93.                     $tfTargetFolder = $folder                 
  94.                 }  
  95.             }  
  96.             else{  
  97.                 “Error Folder Not Discovered”   
  98.                 $tfTargetFolder = $null   
  99.                 break   
  100.             }      
  101.         }   
  102.         if($tfTargetFolder -ne $null){ 
  103.             return $tfTargetFolder.Id.UniqueId.ToString() 
  104.         } 
  105.     } 
  106. } 
  107. #Instance use 
  108. $fldId = FolderIdFromPath -FolderPath “aaaabbbb 
  109. if($fldId -ne “Error Folder Not Discovered“){ 
  110.     $SubFolderId =  new-object Microsoft.Trade.WebServices.Knowledge.FolderId($fldId) 
  111.     $SubFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$SubFolderId) 
  112.  
  113.     $NewPost = New-Object Microsoft.Trade.WebServices.Knowledge.PostItem -ArgumentList $service   
  114.     $NewPost.Topic = “Glad New Yr”   
  115.     $NewPost.Physique = New-Object Microsoft.Trade.WebServices.Knowledge.MessageBody   
  116.     $NewPost.Physique.BodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::HTML   
  117.     $NewPost.Physique.Textual content = “Glad New Yr 
  118.     $NewPost.Save($SubFolder.Id)   
  119.     Write-Host (“Created Put up”)  
  120. }