Friday, December 13, 2013

Using Sender Flags in EWS

Sender Flags are an Outlook Feature that allow you to set a follow up flag on Message when your sending it as well as being able to set a Recipient Followup flag if you want to. (eg)


What happens in Exchange when you set a Sender Flag on a Message is documented in the Informational Flagging Protocol document http://msdn.microsoft.com/en-us/library/cc433487(v=exchg.80).aspx . If you want to do the same thing to a Message your sending in EWS there is nothing in the strongly typed classes to help out so you need to manually set two of the extended properties that are documented in that protocol document before you send the message and the Store will do the rest on submit. (More specifically what happens with the properties involved is documented here http://msdn.microsoft.com/en-us/library/ee217246(v=exchg.80).aspx )

The important properties that are involved with sender flags are the PidTagSwappedToDoData which is used to Store the information about what sender flags your want (eg in Outlook configured via the above dialogue box).  This is a binary property containing an number of Flags, The text for the Message Flags and the DateTime values for the Start and DueDate. The full structure of the property is documented in http://msdn.microsoft.com/en-us/library/ee201575(v=exchg.80).aspx

The DateTime values used in this Property are stored as a 4-byte integer that are expressed as the number of minutes since 00:00:00 on January 1, 1601, in UTC. To get this Integer the method I used is a TimeSpan between the Date you want and January 1, 1601, in UTC, you can then get the TotalMinutes for the timespan as an Integer.

 TimeSpan ts = new DateTime(2013,12,01,0,0,0,0,DateTimeKind.Utc) - new DateTime(1601, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

The other important property is the PidTagSwappedToDoStore which is used by the Store after the Message is submitted. To use this is EWS you need to construct it based on the format documented in http://msdn.microsoft.com/en-us/library/ee203516%28v=exchg.80%29.aspx . The information necessary for this can be obtain through AutoDiscover. 

I've created a Powershell and EWS Sample for setting a Sender Flag on a Message with a Start and DueDate. I've put a download of this script here the code itself looks like

  1. ## Get the Mailbox to Access from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4.   
  5. $SenderFlagText = "This is a Test Blah Blah"  
  6. $FlagStartDate = (Get-Date).AddDays(1)  
  7. $FlagDueDate = (Get-Date).AddDays(7)  
  8.  
  9.  
  10. ## Load Managed API dll    
  11. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"    
  12.    
  13. ## Set Exchange Version    
  14. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2    
  15.    
  16. ## Create Exchange Service Object    
  17. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)    
  18.    
  19. ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials    
  20.    
  21. #Credentials Option 1 using UPN for the windows Account    
  22. $psCred = Get-Credential    
  23. $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  24. $service.Credentials = $creds        
  25.    
  26. #Credentials Option 2    
  27. #service.UseDefaultCredentials = $true    
  28.    
  29. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates    
  30.    
  31. ## Code From http://poshcode.org/624  
  32. ## Create a compilation environment  
  33. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider  
  34. $Compiler=$Provider.CreateCompiler()  
  35. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  36. $Params.GenerateExecutable=$False  
  37. $Params.GenerateInMemory=$True  
  38. $Params.IncludeDebugInformation=$False  
  39. $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null  
  40.   
  41. $TASource=@' 
  42.   namespace Local.ToolkitExtensions.Net.CertificatePolicy{ 
  43.     public class TrustAll : System.Net.ICertificatePolicy { 
  44.       public TrustAll() {  
  45.       } 
  46.       public bool CheckValidationResult(System.Net.ServicePoint sp, 
  47.         System.Security.Cryptography.X509Certificates.X509Certificate cert,  
  48.         System.Net.WebRequest req, int problem) { 
  49.         return true; 
  50.       } 
  51.     } 
  52.   } 
  53. '@   
  54. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)  
  55. $TAAssembly=$TAResults.CompiledAssembly  
  56.  
  57. ## We now create an instance of the TrustAll and attach it to the ServicePointManager  
  58. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")  
  59. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  60.  
  61. ## end code from http://poshcode.org/624  
  62.    
  63. ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use    
  64.    
  65. #CAS URL Option 1 Autodiscover    
  66. $service.AutodiscoverUrl($MailboxName,{$true})    
  67. "Using CAS Server : " + $Service.url     
  68.     
  69. #CAS URL Option 2 Hardcoded    
  70.    
  71. #$uri=[system.URI] "https://casservername/ews/exchange.asmx"    
  72. #$service.Url = $uri      
  73.    
  74. ## Optional section for Exchange Impersonation    
  75.   
  76. function hex2binarray($hexString){  
  77.     $i = 0  
  78.     [byte[]]$binarray = @()  
  79.     while($i -le $hexString.length - 2){  
  80.         $strHexBit = ($hexString.substring($i,2))  
  81.         $binarray += [byte]([Convert]::ToInt32($strHexBit,16))  
  82.         $i = $i + 2  
  83.     }  
  84.     return ,$binarray  
  85. }  
  86.   
  87. function GetAutoDiscoverSettings{  
  88.     param (  
  89.             $adEmailAddress = "$( throw 'emailaddress is a mandatory Parameter' )",  
  90.             $Credentials = "$( throw 'Credentials is a mandatory Parameter' )"  
  91.           )  
  92.     process{  
  93.         $adService = New-Object Microsoft.Exchange.WebServices.AutoDiscover.AutodiscoverService($ExchangeVersion);  
  94.         $adService.Credentials = $Credentials  
  95.         $adService.EnableScpLookup = $false;  
  96.         $adService.RedirectionUrlValidationCallback = {$true}  
  97.         $UserSettings = new-object Microsoft.Exchange.WebServices.Autodiscover.UserSettingName[] 3  
  98.         $UserSettings[0] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDN  
  99.         $UserSettings[1] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::InternalRpcClientServer  
  100.         $UserSettings[2] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDisplayName  
  101.         $adResponse = $adService.GetUserSettings($adEmailAddress , $UserSettings);  
  102.         return $adResponse  
  103.     }  
  104. }  
  105. function GetAddressBookId{  
  106.     param (  
  107.             $AutoDiscoverSettings = "$( throw 'AutoDiscoverSettings is a mandatory Parameter' )"  
  108.           )  
  109.     process{  
  110.         $userdnString = $AutoDiscoverSettings.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDN]  
  111.         $userdnHexChar = $userdnString.ToCharArray();  
  112.         foreach ($element in $userdnHexChar) {$userdnStringHex = $userdnStringHex + [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($element))}  
  113.         $Provider = "00000000DCA740C8C042101AB4B908002B2FE1820100000000000000"  
  114.         $userdnStringHex = $Provider + $userdnStringHex + "00"  
  115.         return $userdnStringHex  
  116.     }  
  117. }  
  118. function GetStoreId{  
  119.     param (  
  120.             $AutoDiscoverSettings = "$( throw 'AutoDiscoverSettings is a mandatory Parameter' )"  
  121.           )  
  122.     process{  
  123.         $userdnString = $AutoDiscoverSettings.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDN]  
  124.         $userdnHexChar = $userdnString.ToCharArray();  
  125.         foreach ($element in $userdnHexChar) {$userdnStringHex = $userdnStringHex + [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($element))}   
  126.         $serverNameString = $AutoDiscoverSettings.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::InternalRpcClientServer]  
  127.         $serverNameHexChar = $serverNameString.ToCharArray();  
  128.         foreach ($element in $serverNameHexChar) {$serverNameStringHex = $serverNameStringHex + [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($element))}  
  129.         $flags = "00000000"  
  130.         $ProviderUID = "38A1BB1005E5101AA1BB08002B2A56C2"  
  131.         $versionFlag = "0000"  
  132.         $DLLFileName = "454D534D44422E444C4C00000000"  
  133.         $WrappedFlags = "00000000"  
  134.         $WrappedProviderUID = "1B55FA20AA6611CD9BC800AA002FC45A"  
  135.         $WrappedType = "0C000000"  
  136.         $StoredIdStringHex = $flags + $ProviderUID + $versionFlag + $DLLFileName + $WrappedFlags + $WrappedProviderUID + $WrappedType + $serverNameStringHex + "00" + $userdnStringHex + "00"  
  137.         return $StoredIdStringHex  
  138.     }  
  139. }  
  140.   
  141.   
  142. function GetPidTagSwappedToDoData {  
  143.     param (  
  144.         $FlagText = "$( throw 'FlagText is a mandatory Parameter' )",  
  145.         $StartTime = "$( throw 'StartTime is a mandatory Parameter' )",  
  146.         $DueTime = "$( throw 'DueTime is a mandatory Parameter' )"  
  147.     )  
  148.     process{  
  149.         $todoTimeFlagged  = "01000000";  
  150.         $PidLidFlagRequest = ""  
  151.         $PidLidFlagRequestHexChar = $FlagText.ToCharArray();  
  152.         $PidLidFlagRequest = [System.BitConverter]::ToString([System.Text.UnicodeEncoding]::Unicode.GetBytes($FlagText)).Replace("-","")  
  153.         #Pad Flag to 512 Bytes  
  154.         for ($padCnt = $PidLidFlagRequest.Length / 2; $padCnt -lt 512; $padCnt++) {  
  155.                 $PidLidFlagRequest = $PidLidFlagRequest + "00";  
  156.         }  
  157.         $stime = New-Object System.DateTime $StartTime.Year,$StartTime.Month,$StartTime.Day,0,0,0,0,Utc  
  158.         $dtime =  New-Object System.DateTime $DueTime.Year,$DueTime.Month,$DueTime.Day,0,0,0,0,Utc  
  159.         $etime = New-Object System.DateTime 1601, 1, 1, 0, 0, 0, 0,Utc  
  160.         [System.TimeSpan]$StartDateTimets = $stime - $etime  
  161.         [System.TimeSpan]$DateDueTimets = $dtime - $etime  
  162.         $HexDateStartTime = [System.Convert]::ToInt64($StartDateTimets.TotalMinutes).ToString("X8");  
  163.         $HexDateStartTime = $HexDateStartTime.Substring(6, 2) + $HexDateStartTime.Substring(4, 2) + $HexDateStartTime.Substring(2, 2) + $HexDateStartTime.Substring(0, 2);  
  164.         $HexDateDueTime = [System.Convert]::ToInt64($DateDueTimets.TotalMinutes).ToString("X8");  
  165.         $HexDateDueTime = $HexDateDueTime.Substring(6, 2) + $HexDateDueTime.Substring(4, 2) + $HexDateDueTime.Substring(2, 2) + $HexDateDueTime.Substring(0, 2);  
  166.         $ulVersion = "01000000";  
  167.         $dwFlags = "79000000"; #dwToDoItem,rtmStartDate,rtmDueDate ,wszFlagTo ,fReminderSet   
  168.         $dwToDoItem = $todoTimeFlagged;  
  169.         $wszFlagTo = $PidLidFlagRequest;  
  170.         $rtmStartDate = $HexDateStartTime;  
  171.         $rtmDueDate = $HexDateDueTime;  
  172.         $rtmReminder = "00000000";  
  173.         $fReminderSet = "00000000";  
  174.         return ($ulVersion + $dwFlags + $dwToDoItem + $wszFlagTo + $rtmStartDate + $rtmDueDate + $rtmReminder + $fReminderSet);  
  175.     }  
  176. }  
  177.  
  178. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  179. $SenderFlagEmail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service    
  180. $SenderFlagEmail.ToRecipients.Add("glenscales@yahoo.com");  
  181. $SenderFlagEmail.Subject = "test";  
  182. $SenderFlagEmail.Body =  New-Object  Microsoft.Exchange.WebServices.Data.MessageBody([Microsoft.Exchange.WebServices.Data.BodyType]::HTML,"test");  
  183. $PR_SWAPPED_TODO_DATA = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x0E2D,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary);  
  184. $PR_SWAPPED_TODO_STORE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x0E2C,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary);  
  185.   
  186. $adset = GetAutoDiscoverSettings -adEmailAddress $MailboxName -Credentials $creds  
  187. $storeID = ""  
  188. if($adset -is [Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverResponse]){  
  189.     Write-Host ("Get StoreId")  
  190.     $storeID = GetStoreId -AutoDiscoverSettings $adset  
  191. }  
  192.   
  193. $senderFlagHex = GetPidTagSwappedToDoData -FlagText $SenderFlagText -StartTime $FlagStartDate  -DueTime $FlagDueDate  
  194.   
  195. $SenderFlagEmail.SetExtendedProperty($PR_SWAPPED_TODO_DATA,(hex2binarray $senderFlagHex));  
  196. $SenderFlagEmail.SetExtendedProperty($PR_SWAPPED_TODO_STORE,(hex2binarray $storeID));  
  197. $SenderFlagEmail.SendAndSaveCopy();  
  198. Write-Host ("Message Sent")  


Wednesday, November 27, 2013

Mapping what Folders are in use in ActiveSync with EWS and Powershell

[***Ingo Gegenwarth has published an better version of this script that handles the changes made recently to ActiveSync on Exchange Online see https://ingogegenwarth.wordpress.com/2018/01/03/eas-folder-mapping/ ]

I came across an interesting Blog post this week from Jim Martin

http://blogs.technet.com/b/tips_from_the_inside/archive/2013/06/17/activesync-mapping-a-collection-id-to-a-mailbox-folder.aspx

which explains how the ActiveSync Folders located under the device folder in ExchangeSyncData folder tree map back to the real Mailbox folder that is being synchronized or accessed via ActiveSync. This information is pretty useful for a number of things so I thought it would be useful to put a script together to automate and create a report of these folder mapping using EWS and Powershell.

How it works is the ExchangeSyncData is located in the NON_IPM_Subtree of a mailbox so a few FindFolder calls are needed to find this folder and then get all the Folders associated with any ActiveSync devices for the Mailbox. The 0x7C030102 property is set on ActiveSync Collection folder and as in Jim's post if you take the first byte and last byte off the value from this property you have the HexEntryId of the folder. In EWS you can then use convertId to covert it to the ewsID of the Folder and then bind to the FolderId to work out which folder is being synced. Basically what you end up with is a report that looks like this

The LastModified column is the time the ActiveSync folder was last modified which should relate to the last time the folder was accessed over ActiveSync (from my observation anyway). The Device column just comes from the DisplayName of the Root folder from the device

I've put a download of this script here the code itself looks like

  1. ## Get the Mailbox to Access from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4. $AsFolderReport = @()  
  5.   
  6. ## Load Managed API dll    
  7. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"    
  8.     
  9. ## Set Exchange Version    
  10. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2    
  11.     
  12. ## Create Exchange Service Object    
  13. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)    
  14.     
  15. ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials    
  16.     
  17. #Credentials Option 1 using UPN for the windows Account    
  18. $psCred = Get-Credential    
  19. $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  20. $service.Credentials = $creds        
  21.     
  22. #Credentials Option 2    
  23. #service.UseDefaultCredentials = $true    
  24.     
  25. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates    
  26.     
  27. ## Code From http://poshcode.org/624  
  28. ## Create a compilation environment  
  29. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider  
  30. $Compiler=$Provider.CreateCompiler()  
  31. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  32. $Params.GenerateExecutable=$False  
  33. $Params.GenerateInMemory=$True  
  34. $Params.IncludeDebugInformation=$False  
  35. $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null  
  36.   
  37. $TASource=@' 
  38.   namespace Local.ToolkitExtensions.Net.CertificatePolicy{ 
  39.     public class TrustAll : System.Net.ICertificatePolicy { 
  40.       public TrustAll() {  
  41.       } 
  42.       public bool CheckValidationResult(System.Net.ServicePoint sp, 
  43.         System.Security.Cryptography.X509Certificates.X509Certificate cert,  
  44.         System.Net.WebRequest req, int problem) { 
  45.         return true; 
  46.       } 
  47.     } 
  48.   } 
  49. '@   
  50. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)  
  51. $TAAssembly=$TAResults.CompiledAssembly  
  52.   
  53. ## We now create an instance of the TrustAll and attach it to the ServicePointManager  
  54. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")  
  55. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  56.   
  57. ## end code from http://poshcode.org/624  
  58.     
  59. ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use    
  60.     
  61. #CAS URL Option 1 Autodiscover    
  62. $service.AutodiscoverUrl($MailboxName,{$true})    
  63. "Using CAS Server : " + $Service.url     
  64.      
  65. #CAS URL Option 2 Hardcoded    
  66.     
  67. #$uri=[system.URI] "https://casservername/ews/exchange.asmx"    
  68. #$service.Url = $uri      
  69.     
  70. ## Optional section for Exchange Impersonation    
  71.     
  72. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  73. # Bind to the MsgFolderRoot folder    
  74. $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)     
  75. $MsgRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
  76.   
  77. function ConvertId{      
  78.     param (  
  79.             $HexId = "$( throw 'HexId is a mandatory Parameter' )"  
  80.           )  
  81.     process{  
  82.         $aiItem = New-Object Microsoft.Exchange.WebServices.Data.AlternateId        
  83.         $aiItem.Mailbox = $MailboxName        
  84.         $aiItem.UniqueId = $HexId     
  85.         $aiItem.Format = [Microsoft.Exchange.WebServices.Data.IdFormat]::HexEntryId        
  86.         $convertedId = $service.ConvertId($aiItem, [Microsoft.Exchange.WebServices.Data.IdFormat]::EwsId)   
  87.         return $convertedId.UniqueId  
  88.     }  
  89. }  
  90.   
  91. function GetFolderPath{  
  92.     param (  
  93.         $EWSFolder = "$( throw 'Folder is a mandatory Parameter' )"  
  94.     )  
  95.     process{  
  96.         $foldpathval = $null    
  97.         $PR_Folder_Path = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26293, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);    
  98.         if ($EWSFolder.TryGetProperty($PR_Folder_Path,[ref] $foldpathval))    
  99.         {    
  100.             $binarry = [Text.Encoding]::UTF8.GetBytes($foldpathval)    
  101.             $hexArr = $binarry | ForEach-Object { $_.ToString("X2") }    
  102.             $hexString = $hexArr -join ''    
  103.         $hexString = $hexString.Replace("EFBFBE""5C")    
  104.             $fpath = ConvertToString($hexString)   
  105.         return $fpath  
  106.         }    
  107.     }  
  108. }  
  109. $fldMappingHash = @{}  
  110. #Define the FolderView used for Export should not be any larger then 1000 folders due to throttling    
  111. $fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(1)    
  112. #Deep Transval will ensure all folders in the search path are returned    
  113. $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow;    
  114. #The Search filter will exclude any Search Folders    
  115. $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"ExchangeSyncData")    
  116. $asFolderRoot = $Service.FindFolders($MsgRoot.Id,$sfSearchFilter,$fvFolderView)    
  117. if($asFolderRoot.Folders.Count -eq 1){  
  118.     #Define Function to convert String to FolderPath    
  119.     function ConvertToString($ipInputString){    
  120.         $Val1Text = ""    
  121.         for ($clInt=0;$clInt -lt $ipInputString.length;$clInt++){    
  122.                 $Val1Text = $Val1Text + [Convert]::ToString([Convert]::ToChar([Convert]::ToInt32($ipInputString.Substring($clInt,2),16)))    
  123.                 $clInt++    
  124.         }    
  125.         return $Val1Text    
  126.     }   
  127.       
  128.     #Define Extended properties    
  129.     $PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);    
  130.     #Define the FolderView used for Export should not be any larger then 1000 folders due to throttling    
  131.     $fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)    
  132.     #Deep Transval will ensure all folders in the search path are returned    
  133.     $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;    
  134.     $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  135.     $PR_Folder_Path = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26293, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);    
  136.     $CollectionIdProp = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x7C03, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  137.     $LastModifiedTime = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x3008, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::SystemTime)  
  138.     #Add Properties to the  Property Set    
  139.     $psPropertySet.Add($PR_Folder_Path);    
  140.     $psPropertySet.Add($CollectionIdProp);  
  141.     $psPropertySet.Add($LastModifiedTime);    
  142.     $fvFolderView.PropertySet = $psPropertySet;    
  143.     #The Search filter will exclude any Search Folders    
  144.     $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")    
  145.     $fiResult = $null    
  146.     #The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox    
  147.     do {    
  148.         $fiResult = $Service.FindFolders($asFolderRoot.Folders[0].Id,$sfSearchFilter,$fvFolderView)    
  149.         foreach($ffFolder in $fiResult.Folders){   
  150.             if(!$fldMappingHash.ContainsKey($ffFolder.Id.UniqueId)){  
  151.                 $fldMappingHash.Add($ffFolder.Id.UniqueId,$ffFolder)  
  152.             }  
  153.             $asFolderPath = ""  
  154.             $asFolderPath = (GetFolderPath -EWSFolder $ffFolder)  
  155.             "FolderPath : " + $asFolderPath  
  156.             $collectVal = $null  
  157.             if($ffFolder.TryGetProperty($CollectionIdProp,[ref]$collectVal)){  
  158.                 $HexEntryId = [System.BitConverter]::ToString($collectVal).Replace("-","").Substring(2)  
  159.                 $ewsFolderId = ConvertId -HexId ($HexEntryId.SubString(0,($HexEntryId.Length-2)))  
  160.                 try{  
  161.                     $fldReport = "" | Select Mailbox,Device,AsFolderPath,MailboxFolderPath,LastModified  
  162.                     $fldReport.Mailbox = $MailboxName  
  163.                     $fldReport.Device = $fldMappingHash[$ffFolder.ParentFolderId.UniqueId].DisplayName  
  164.                     $fldReport.AsFolderPath = $asFolderPath  
  165.                     $folderMapId= new-object Microsoft.Exchange.WebServices.Data.FolderId($ewsFolderId)     
  166.                     $MappedFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderMapId,$psPropertySet)  
  167.                     $MappedFolderPath = (GetFolderPath -EWSFolder $MappedFolder)  
  168.                     $fldReport.MailboxFolderPath = $MappedFolderPath  
  169.                     $LastModifiedVal = $null  
  170.                     if($ffFolder.TryGetProperty($LastModifiedTime,[ref]$LastModifiedVal)){  
  171.                         Write-Host ("Last-Modified : " +  $LastModifiedVal.ToLocalTime().ToString())  
  172.                         $fldReport.LastModified = $LastModifiedVal.ToLocalTime().ToString()  
  173.                     }  
  174.                     Write-Host $MappedFolderPath  
  175.                     $AsFolderReport += $fldReport  
  176.                 }  
  177.                 catch{  
  178.                       
  179.                 }  
  180.                 $ewsFolderId  
  181.             }  
  182.             #Process folder here  
  183.         }   
  184.         $fvFolderView.Offset += $fiResult.Folders.Count  
  185.     }while($fiResult.MoreAvailable -eq $true)    
  186.       
  187.       
  188. }  
  189. $reportFile = "c:\temp\$MailboxName-asFolders.csv"  
  190. $AsFolderReport | Export-Csv -NoTypeInformation -Path $reportFile  
  191. Write-Host ("Report wrtten to " + $reportFile)  

Friday, November 15, 2013

Turning off the Reading / Preview Pane on all folders in OWA in Exchange 2013 and Exchange Online in EWS with Powershell

Exchange 2013 and Exchange Online has a cool new feature in OWA to let you control the Reading pane setting on all folders in your Mailbox in OWA eg. (in previous versions you had to manage it yourself per folder).

When you set this setting in OWA and check the "Apply to all folders" box, in your mailbox it sets a configuration setting called GlobalReadingPanePosition in the OWA.UserOptions FAI (Folder Associated Item) in the Non_IPM Root of your Mailbox. The following values control the position of the Reading Pane on all folders

0 - Hides the Reading Pane
1 - Shows the Reading Pane on the Rights (this is the default)
2 - Shows the Reading Pane at the bottom

To Access this FAI Item in EWS you use the UserConfiguration class which makes accessing the Roaming Dictionary structure where this particular value is held really easy. Eg the following script will turn the Reading pane off on all Folders in the mailbox its ran against. I've put a download of this script here the code itself looks like

  1. ## Get the Mailbox to Access from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4.   
  5. ## Load Managed API dll    
  6. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"    
  7.     
  8. ## Set Exchange Version    
  9. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2    
  10.     
  11. ## Create Exchange Service Object    
  12. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)    
  13.     
  14. ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials    
  15.     
  16. #Credentials Option 1 using UPN for the windows Account    
  17. $psCred = Get-Credential    
  18. $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  19. $service.Credentials = $creds        
  20.     
  21. #Credentials Option 2    
  22. #service.UseDefaultCredentials = $true    
  23.     
  24. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates    
  25.     
  26. ## Code From http://poshcode.org/624  
  27. ## Create a compilation environment  
  28. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider  
  29. $Compiler=$Provider.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 Local.ToolkitExtensions.Net.CertificatePolicy{ 
  38.     public class TrustAll : System.Net.ICertificatePolicy { 
  39.       public TrustAll() {  
  40.       } 
  41.       public bool CheckValidationResult(System.Net.ServicePoint sp, 
  42.         System.Security.Cryptography.X509Certificates.X509Certificate cert,  
  43.         System.Net.WebRequest req, int problem) { 
  44.         return true; 
  45.       } 
  46.     } 
  47.   } 
  48. '@   
  49. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)  
  50. $TAAssembly=$TAResults.CompiledAssembly  
  51.   
  52. ## We now create an instance of the TrustAll and attach it to the ServicePointManager  
  53. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")  
  54. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  55.   
  56. ## end code from http://poshcode.org/624  
  57.     
  58. ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use    
  59.     
  60. #CAS URL Option 1 Autodiscover    
  61. $service.AutodiscoverUrl($MailboxName,{$true})    
  62. "Using CAS Server : " + $Service.url     
  63.      
  64. #CAS URL Option 2 Hardcoded    
  65.     
  66. #$uri=[system.URI] "https://casservername/ews/exchange.asmx"    
  67. #$service.Url = $uri      
  68.     
  69. ## Optional section for Exchange Impersonation    
  70.     
  71. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  72.   
  73. $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)       
  74. #Specify the Root folder where the FAI Item is    
  75. $UsrConfig = [Microsoft.Exchange.WebServices.Data.UserConfiguration]::Bind($service"OWA.UserOptions"$folderid, [Microsoft.Exchange.WebServices.Data.UserConfigurationProperties]::All)    
  76. if($UsrConfig.Dictionary.ContainsKey("GlobalReadingPanePosition")){    
  77.     $UsrConfig.Dictionary["GlobalReadingPanePosition"] = 0    
  78. }    
  79. else{    
  80.     $UsrConfig.Dictionary.Add("GlobalReadingPanePosition",0)    
  81. }  
  82. $UsrConfig.Update()    
  83. "Item updated"  


Thursday, October 31, 2013

Creating a Shared Calendar Shortcut (WunderBar Link) with EWS and Powershell

Shared Calendar Shortcuts eg



are an Outlook and OWA  feature that can be handy to automate if you need to deploy a number of these to new (or existing) mailboxes and you don't want to go through the invitation/accept procedure or manually adding each shortcut.

While there are no supported operations in EWS for creating these type of objects, it can be achieved by setting the Extended MAPI properties that constitute the shortcut. The downside of this is that it wouldn't ever be considered supported if it all goes horribly wrong. The properties involved in the shortcut are documented in the following protocol document http://msdn.microsoft.com/en-us/library/ee157359(v=exchg.80).aspx.

For a couple of these properties the values you need to get can't be obtained directly in EWS so some others tricks are needed.

The PidTagWlinkAddressBookEID property contains the MAPI address Book EntryId for the shared Calendar your connecting to. The Address Book EntryID format is documented here http://msdn.microsoft.com/en-us/library/ee160588%28v=exchg.80%29.aspx so to construct this in EWS you need to get the LegacyExchangeDN from AutoDiscover and then appended the ProviderUID,Flag and Type information which for a normal user will be the same. (Note if you are trying to connect to an object other then a user mailbox.

The PidTagWlinkAddressBookStoreEID proeprty contains the MAPI StoreEntryID of the users where your creating the ShortCut. While you can get this property in EWS the value you get isn't correct because EWS uses different wrapper and providerID values. So instead using the format documented in http://msdn.microsoft.com/en-us/library/ee203516%28v=exchg.80%29.aspx you can construct this which involves getting the LegacyExchangeDN and the ServerName for Autodiscover and then building the identifier.

The other properties are pretty static for shared calendars.

The ShortCuts them selves are saved in a Non_IPM_Subtree folder called common views

(Special thanks also to Neil Doody for helping with property definitions in this post and script)

So the following script takes two commandline parameter the first is the Mailbox where you want the shortcut to be created and the second is the Mailbox which has the Calendar you want the shortcut to point to so you would run it like

./createWBarCal.ps1 user@domain.com target@domain.com

 The script will check the CommonViews folder to see if a SharedFolder shortcut already exists for the target Mailbox AddressId and FolderType and if no ShortCut exists and it will attempt to create one.

As i mentioned before as this script is completely unsupported and for the most part untested and should only be considered safe for testing in a development\test environment. It also assume you have Autodiscover working if not your this isn't going to work well.

 I've put a download of the script here the code itself looks like

  1. ## Get the Mailbox to Access from the 1st commandline argument  
  2. $TargetCalendarMailbox = $args[1]  
  3. $MailboxName = $args[0]  
  4.   
  5. ## Load Managed API dll    
  6. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"    
  7.     
  8. ## Set Exchange Version    
  9. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2    
  10.     
  11. ## Create Exchange Service Object    
  12. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)    
  13.     
  14. ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials    
  15.     
  16. #Credentials Option 1 using UPN for the windows Account    
  17. $psCred = Get-Credential    
  18. $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  19. $service.Credentials = $creds        
  20.     
  21. #Credentials Option 2    
  22. #service.UseDefaultCredentials = $true    
  23.     
  24. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates    
  25.     
  26. ## Code From http://poshcode.org/624  
  27. ## Create a compilation environment  
  28. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider  
  29. $Compiler=$Provider.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 Local.ToolkitExtensions.Net.CertificatePolicy{ 
  38.     public class TrustAll : System.Net.ICertificatePolicy { 
  39.       public TrustAll() {  
  40.       } 
  41.       public bool CheckValidationResult(System.Net.ServicePoint sp, 
  42.         System.Security.Cryptography.X509Certificates.X509Certificate cert,  
  43.         System.Net.WebRequest req, int problem) { 
  44.         return true; 
  45.       } 
  46.     } 
  47.   } 
  48. '@   
  49. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)  
  50. $TAAssembly=$TAResults.CompiledAssembly  
  51.   
  52. ## We now create an instance of the TrustAll and attach it to the ServicePointManager  
  53. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")  
  54. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  55.   
  56. ## end code from http://poshcode.org/624  
  57.     
  58. ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use    
  59.     
  60. #CAS URL Option 1 Autodiscover    
  61. $service.AutodiscoverUrl($MailboxName,{$true})    
  62. "Using CAS Server : " + $Service.url     
  63.      
  64. #CAS URL Option 2 Hardcoded    
  65.     
  66. #$uri=[system.URI] "https://casservername/ews/exchange.asmx"    
  67. #$service.Url = $uri      
  68.     
  69. ## Optional section for Exchange Impersonation    
  70.     
  71. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  72.   
  73. function GetAutoDiscoverSettings{  
  74.     param (  
  75.             $adEmailAddress = "$( throw 'emailaddress is a mandatory Parameter' )",  
  76.             $Credentials = "$( throw 'Credentials is a mandatory Parameter' )"  
  77.           )  
  78.     process{  
  79.         $adService = New-Object Microsoft.Exchange.WebServices.AutoDiscover.AutodiscoverService($ExchangeVersion);  
  80.         $adService.Credentials = $Credentials  
  81.         $adService.EnableScpLookup = $false;  
  82.         $adService.RedirectionUrlValidationCallback = {$true}  
  83.         $UserSettings = new-object Microsoft.Exchange.WebServices.Autodiscover.UserSettingName[] 3  
  84.         $UserSettings[0] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDN  
  85.         $UserSettings[1] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::InternalRpcClientServer  
  86.         $UserSettings[2] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDisplayName  
  87.         $adResponse = $adService.GetUserSettings($adEmailAddress , $UserSettings);  
  88.         return $adResponse  
  89.     }  
  90. }  
  91. function GetAddressBookId{  
  92.     param (  
  93.             $AutoDiscoverSettings = "$( throw 'AutoDiscoverSettings is a mandatory Parameter' )"  
  94.           )  
  95.     process{  
  96.         $userdnString = $AutoDiscoverSettings.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDN]  
  97.         $userdnHexChar = $userdnString.ToCharArray();  
  98.         foreach ($element in $userdnHexChar) {$userdnStringHex = $userdnStringHex + [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($element))}  
  99.         $Provider = "00000000DCA740C8C042101AB4B908002B2FE1820100000000000000"  
  100.         $userdnStringHex = $Provider + $userdnStringHex + "00"  
  101.         return $userdnStringHex  
  102.     }  
  103. }  
  104. function GetStoreId{  
  105.     param (  
  106.             $AutoDiscoverSettings = "$( throw 'AutoDiscoverSettings is a mandatory Parameter' )"  
  107.           )  
  108.     process{  
  109.         $userdnString = $AutoDiscoverSettings.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDN]  
  110.         $userdnHexChar = $userdnString.ToCharArray();  
  111.         foreach ($element in $userdnHexChar) {$userdnStringHex = $userdnStringHex + [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($element))}   
  112.         $serverNameString = $AutoDiscoverSettings.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::InternalRpcClientServer]  
  113.         $serverNameHexChar = $serverNameString.ToCharArray();  
  114.         foreach ($element in $serverNameHexChar) {$serverNameStringHex = $serverNameStringHex + [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($element))}  
  115.         $flags = "00000000"  
  116.         $ProviderUID = "38A1BB1005E5101AA1BB08002B2A56C2"  
  117.         $versionFlag = "0000"  
  118.         $DLLFileName = "454D534D44422E444C4C00000000"  
  119.         $WrappedFlags = "00000000"  
  120.         $WrappedProviderUID = "1B55FA20AA6611CD9BC800AA002FC45A"  
  121.         $WrappedType = "0C000000"  
  122.         $StoredIdStringHex = $flags + $ProviderUID + $versionFlag + $DLLFileName + $WrappedFlags + $WrappedProviderUID + $WrappedType + $serverNameStringHex + "00" + $userdnStringHex + "00"  
  123.         return $StoredIdStringHex  
  124.     }  
  125. }  
  126.   
  127.   
  128. function hex2binarray($hexString){  
  129.     $i = 0  
  130.     [byte[]]$binarray = @()  
  131.     while($i -le $hexString.length - 2){  
  132.         $strHexBit = ($hexString.substring($i,2))  
  133.         $binarray += [byte]([Convert]::ToInt32($strHexBit,16))  
  134.         $i = $i + 2  
  135.     }  
  136.     return ,$binarray  
  137. }  
  138. function ConvertId($EWSid){      
  139.     $aiItem = New-Object Microsoft.Exchange.WebServices.Data.AlternateId        
  140.     $aiItem.Mailbox = $MailboxName        
  141.     $aiItem.UniqueId = $EWSid     
  142.     $aiItem.Format = [Microsoft.Exchange.WebServices.Data.IdFormat]::EWSId;        
  143.     return $service.ConvertId($aiItem, [Microsoft.Exchange.WebServices.Data.IdFormat]::StoreId)       
  144. }   
  145.   
  146. #PropDefs   
  147. $pidTagStoreEntryId = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(4091, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  148. $PidTagNormalizedSubject = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x0E1D,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);   
  149. $PidTagWlinkType = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6849, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  150. $PidTagWlinkFlags = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x684A, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  151. $PidTagWlinkOrdinal = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x684B, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  152. $PidTagWlinkFolderType = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x684F, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  153. $PidTagWlinkSection = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6852, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  154. $PidTagWlinkGroupHeaderID = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6842, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  155. $PidTagWlinkSaveStamp = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6847, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  156. $PidTagWlinkGroupName = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6851, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)  
  157. $PidTagWlinkStoreEntryId = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x684E, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  158. $PidTagWlinkGroupClsid = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6850, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  159. $PidTagWlinkEntryId = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x684C, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  160. $PidTagWlinkRecordKey = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x684D, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  161. $PidTagWlinkCalendarColor = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6853, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  162. $PidTagWlinkAddressBookEID = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6854,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  163. $PidTagWlinkROGroupType = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6892,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  164. $PidTagWlinkAddressBookStoreEID = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6891,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  165.   
  166.   
  167. #Get the TargetUsers Calendar  
  168. # Bind to the Calendar Folder  
  169. $fldPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  170. $fldPropset.Add($pidTagStoreEntryId);  
  171. $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$TargetCalendarMailbox)     
  172. $TargetCalendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid,$fldPropset)  
  173. #Check for existing ShortCut for TargetMailbox  
  174. #Get AddressBook Id for TargetUser  
  175. Write-Host ("Getting Autodiscover Settings Target")  
  176. Write-Host ("Getting Autodiscover Settings Mailbox")  
  177. $adset = GetAutoDiscoverSettings -adEmailAddress $MailboxName -Credentials $creds  
  178. $storeID = ""  
  179. if($adset -is [Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverResponse]){  
  180.     Write-Host ("Get StoreId")  
  181.     $storeID = GetStoreId -AutoDiscoverSettings $adset  
  182. }  
  183. $adset = $null  
  184. $abTargetABEntryId = ""  
  185. $adset = GetAutoDiscoverSettings -adEmailAddress $TargetCalendarMailbox -Credentials $creds  
  186. if($adset -is [Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverResponse]){  
  187.     Write-Host ("Get AB Id")  
  188.     $abTargetABEntryId = GetAddressBookId -AutoDiscoverSettings $adset  
  189.     $SharedUserDisplayName =  $adset.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDisplayName]  
  190. }  
  191. Write-Host ("Getting CommonVeiwFolder")  
  192. #Get CommonViewFolder  
  193. $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)     
  194. $tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)    
  195. $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1)   
  196. $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"Common Views")   
  197. $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)   
  198. if ($findFolderResults.TotalCount -gt 0){   
  199.     $ExistingShortCut = $false  
  200.     $cvCommonViewsFolder = $findFolderResults.Folders[0]  
  201.     #Define ItemView to retrive just 1000 Items      
  202.     #Find Items that are unread  
  203.     $psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  204.     $psPropset.add($PidTagWlinkAddressBookEID)  
  205.     $psPropset.add($PidTagWlinkFolderType)  
  206.     $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)     
  207.     $ivItemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Associated  
  208.     $ivItemView.PropertySet = $psPropset  
  209.     $fiItems = $service.FindItems($cvCommonViewsFolder.Id,$ivItemView)      
  210.     foreach($Item in $fiItems.Items){  
  211.         $aeidVal = $null  
  212.         if($Item.TryGetProperty($PidTagWlinkAddressBookEID,[ref]$aeidVal)){  
  213.                 $fldType = $null  
  214.                 if($Item.TryGetProperty($PidTagWlinkFolderType,[ref]$fldType)){  
  215.                     if([System.BitConverter]::ToString($fldType).Replace("-","") -eq "0278060000000000C000000000000046"){  
  216.                         if([System.BitConverter]::ToString($aeidVal).Replace("-","") -eq $abTargetABEntryId){  
  217.                             $ExistingShortCut = $true  
  218.                             Write-Host "Found existing Shortcut"  
  219.                             ###$Item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete)  
  220.                         }  
  221.                     }  
  222.                 }  
  223.             }                               
  224.     }  
  225.     if($ExistingShortCut -eq $false){  
  226.         If($storeID.length -gt 5 -band $abTargetABEntryId.length -gt 5){  
  227.             $objWunderBarLink = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service    
  228.             $objWunderBarLink.Subject = $SharedUserDisplayName    
  229.             $objWunderBarLink.ItemClass = "IPM.Microsoft.WunderBar.Link"    
  230.             $objWunderBarLink.SetExtendedProperty($PidTagWlinkAddressBookEID,(hex2binarray $abTargetABEntryId))    
  231.             $objWunderBarLink.SetExtendedProperty($PidTagWlinkAddressBookStoreEID,(hex2binarray $storeID))    
  232.             $objWunderBarLink.SetExtendedProperty($PidTagWlinkCalendarColor,-1)  
  233.             $objWunderBarLink.SetExtendedProperty($PidTagWlinkFlags,0)  
  234.             $objWunderBarLink.SetExtendedProperty($PidTagWlinkGroupName,"Shared Calendars")  
  235.             $objWunderBarLink.SetExtendedProperty($PidTagWlinkFolderType,(hex2binarray "0278060000000000C000000000000046"))    
  236.             $objWunderBarLink.SetExtendedProperty($PidTagWlinkGroupClsid,(hex2binarray "B9F0060000000000C000000000000046"))    
  237.             $objWunderBarLink.SetExtendedProperty($PidTagWlinkROGroupType,-1)  
  238.             $objWunderBarLink.SetExtendedProperty($PidTagWlinkSection,3)    
  239.             $objWunderBarLink.SetExtendedProperty($PidTagWlinkType,2)    
  240.             $objWunderBarLink.IsAssociated = $true  
  241.             $objWunderBarLink.Save($findFolderResults.Folders[0].Id)  
  242.             Write-Host ("ShortCut Created for - " + $SharedUserDisplayName)  
  243.         }  
  244.         else{  
  245.             Write-Host ("Error with Id's")  
  246.         }  
  247.     }  
  248. }