Setting the Color of a calendar utilizing EWS and Powershell in Alternate

Setting the Color of a calendar utilizing EWS and Powershell in Alternate In Outlook and OWA if you find yourself utilizing the A number of calendar view function you possibly can assign totally different colours to calendars inside that view. (By default the calendar colour is about to computerized which suggests a colour is assigned at random) eg

Whenever you add a Shared Calendar to your Mailbox you create a Calendar WunderBar Navigational shortcut that are documented https://msdn.microsoft.com/en-us/library/ee202589(v=exchg.80).aspx . The colour that these shortcuts can be set to is about through the PidTagWlinkCalendarColor property https://msdn.microsoft.com/en-us/library/ee200641(v=exchg.80).aspx .

So if you wish to set the colour of certainly one of these shortcuts with a script it does require a little bit bit of labor. First to get the shortcut Objects you could first get the CommonViews folder after which enumerate the FAI Objects assortment the place these shortcuts are saved. Now relying if you wish to the Set the Default Folder Shortcut or a Shared Folder Shortcut (or the opposite calendars Shortcut for Room mailboxes and the like) you could take a look at a couple of totally different properties. On Shared Calendar Shortcuts you could take a look at the PidTagWlinkAddressBookEID property whereas on the default folder you must take a look at the PidTagWlinkEntryId property.

I’ve written a powershell module for doing this and posted it up on GitHub so with this module you possibly can . The CalendarColor enun is mildly correct

Set the Colour of the Default Calendar Folder in a Mailbox

Set-DefaultCalendarFolderShortCut -MailboxName [email protected] -CalendarColor Yellow

 (To only get the EWS Navigation Merchandise for the default calendar folder use)

Get-DefaultCalendarFolderShortCut -MailboxName [email protected]

To Set the Colour of a Shared Calendar folder use

Set-SharedCalendarFolderShortCut -MailboxName [email protected] -SharedCalendarMailboxName [email protected] -CalendarColor Yellow

You’ll be able to download a replica of the module from right here the code itself appears to be like like

perform Join-Alternate{ 
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=1, Obligatory=$true)] [System.Management.Automation.PSCredential]$Credentials
)
Start
{
Load-EWSManagedAPI

## Set Alternate Model
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Alternate2010_SP1

## Create Alternate Service Object
$service = New-Object Microsoft.Alternate.WebServices.Knowledge.ExchangeService($ExchangeVersion)

## Set Credentials to make use of two choices are availible Option1 to make use of explict credentials or Possibility 2 use the Default (logged On) credentials

#Credentials Possibility 1 utilizing UPN for the home windows Account
#$psCred = Get-Credential
$creds = New-Object System.Internet.NetworkCredential($Credentials.UserName.ToString(),$Credentials.GetNetworkCredential().password.ToString())
$service.Credentials = $creds
#Credentials Possibility 2
#service.UseDefaultCredentials = $true
#$service.TraceEnabled = $true
## Select to disregard any SSL Warning points brought on by Self Signed Certificates

Deal with-SSL

## Set the URL of the CAS (Shopper Entry Server) to make use of two choices are availbe to make use of Autodiscover to search out the CAS URL or Hardcode the CAS to make use of

#CAS URL Possibility 1 Autodiscover
$service.AutodiscoverUrl($MailboxName,{$true})
Write-host ("Using CAS Server : " + $Service.url)

#CAS URL Possibility 2 Hardcoded

#$uri=[system.URI] "https://casservername/ews/exchange.asmx"
#$service.Url = $uri

## Elective part for Alternate Impersonation

#$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
if(!$service.URL){
throw "Error connecting to EWS"
}
else
{
return $service
}
}
}

perform Load-EWSManagedAPI{
param(
)
Start
{
## Load Managed API dll
###CHECK FOR EWS MANAGED API, IF PRESENT IMPORT THE HIGHEST VERSION EWS DLL, ELSE EXIT
$EWSDLL = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path 'Registry::HKEY_LOCAL_MACHINESOFTWAREMicrosoftExchangeWeb Companies'|Type-Object Title -Descending| Choose-Object -First 1 -ExpandProperty Title)).'Set up Listing') + "Microsoft.Exchange.WebServices.dll")
if (Take a look at-Path $EWSDLL)
{
Import-Module $EWSDLL
}
else
{
"$(get-date -format yyyyMMddHHmmss):"
"This script requires the EWS Managed API 1.2 or later."
"Please download and install the current version of the EWS Managed API from"
"http://go.microsoft.com/fwlink/?LinkId=255472"
""
"Exiting Script."
exit
}
}
}

perform Deal with-SSL{
param(
)
Start
{
## Code From http://poshcode.org/624
## Create a compilation atmosphere
$Supplier=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Supplier.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null

$TASource=@'
namespace Native.ToolkitExtensions.Internet.CertificatePolicy{
public class TrustAll : System.Internet.ICertificatePolicy {
public TrustAll() {
}
public bool CheckValidationResult(System.Internet.ServicePoint sp,
System.Safety.Cryptography.X509Certificates.X509Certificates cert,
System.Internet.WebRequest req, int downside) {
return true;
}
}
}
'@
$TAResults=$Supplier.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly

## We now create an occasion of the TrustAll and fix it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll

## finish code from http://poshcode.org/624

}
}

perform Get-FolderFromPath{
param (
[Parameter(Place=0, Obligatory=$true)] [string]$FolderPath,
[Parameter(Place=1, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=2, Obligatory=$true)] [Microsoft.Exchange.WebServices.Data.ExchangeService]$service,
[Parameter(Place=3, Obligatory=$false)] [Microsoft.Exchange.WebServices.Data.PropertySet]$PropertySet
)
course of{
## Discover and Bind to Folder primarily based on Path
#Outline the trail to look needs to be seperated with
#Bind to the MSGFolder Root
$folderid = new-object Microsoft.Alternate.WebServices.Knowledge.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
$tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
#Cut up the Search path into an array
$fldArray = $FolderPath.Cut up("")
#Loop by way of the Cut up Array and do a Seek for every stage of folder
for ($lint = 1; $lint -lt $fldArray.Size; $lint++) {
#Carry out search primarily based on the displayname of every folder stage
$fvFolderView = new-object Microsoft.Alternate.WebServices.Knowledge.FolderView(1)
if(![string]::IsNullOrEmpty($PropertySet)){
$fvFolderView.PropertySet = $PropertySet
}
$SfSearchFilter = new-object Microsoft.Alternate.WebServices.Knowledge.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$fldArray[$lint])
$findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)
if ($findFolderResults.TotalCount -gt 0){
foreach($folder in $findFolderResults.Folders){
$tfTargetFolder = $folder
}
}
else{
Write-host ("Error Folder Not Found check path and try again")
$tfTargetFolder = $null
break
}
}
if($tfTargetFolder -ne $null){
return [Microsoft.Exchange.WebServices.Data.Folder]$tfTargetFolder
}
else{
throw ("Folder Not found")
}
}
}

if (-not ([System.Management.Automation.PSTypeName]'CalendarColor').Sort)
{
Add-Sort -TypeDefinition @"
public enum CalendarColor
{
Automatch = -1,
Blue = 0,
Inexperienced = 1,
Peach = 2,
Grey = 3,
Teal = 4,
Pink = 5,
Olive = 6,
Pink = 7,
Orange = 8,
Purple = 9,
Tan = 10,
Light_Green = 12,
Yellow = 13,
Eton_Blue = 14
}
"@
}

perform Get-CommonViews{
param (
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=1, Obligatory=$true)] [Microsoft.Exchange.WebServices.Data.ExchangeService]$service
)
course of{
$folderid = new-object Microsoft.Alternate.WebServices.Knowledge.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)
$tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$fvFolderView = new-object Microsoft.Alternate.WebServices.Knowledge.FolderView(1)
$SfSearchFilter = new-object Microsoft.Alternate.WebServices.Knowledge.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"Common Views")
$findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)
if ($findFolderResults.TotalCount -gt 0){
return $findFolderResults.Folders[0]
}
else{
return $null
}
}
}


perform Exec-FindCalendarShortCut{
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=1, Obligatory=$true)] [Microsoft.Exchange.WebServices.Data.ExchangeService]$service
)
Start
{
# Bind to the Calendar Folder
$folderid= new-object Microsoft.Alternate.WebServices.Knowledge.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MailboxName)
$psPropset= new-object Microsoft.Alternate.WebServices.Knowledge.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$pidTagEntryId = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x0FFF, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkEntryId = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x684C, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkGroupName = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6851, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
$PidTagWlinkCalendarColor = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6853, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
$psPropset.Add($pidTagEntryId)
$psPropset.Add($PidTagWlinkGroupName)
$psPropset.Add($PidTagWlinkCalendarColor)
$Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid,$psPropset)
$CalendarEntryId = $null
[Void]$Calendar.TryGetProperty($pidTagEntryId,[ref]$CalendarEntryId)
$SfSearchFilter = new-object Microsoft.Alternate.WebServices.Knowledge.SearchFilter+IsEqualTo($PidTagWlinkEntryId,[System.Convert]::ToBase64String($CalendarEntryId))
#Get the HexId
$CommonViewFolder = Get-CommonViews -MailboxName $MailboxName -service $service
$retrunItem = @()
if($CommonViewFolder -ne $null){
#Outline ItemView to retrive simply 1000 Objects
$ivItemView = New-Object Microsoft.Alternate.WebServices.Knowledge.ItemView(1000)
$ivItemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Related
$ivItemView.PropertySet = $psPropset
$fiItems = $null
do{
$fiItems = $service.FindItems($CommonViewFolder.Id,$SfSearchFilter,$ivItemView)
#[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)
foreach($Merchandise in $fiItems.Objects){
$retrunItem += $Merchandise
}
$ivItemView.Offset += $fiItems.Objects.Depend
}whereas($fiItems.MoreAvailable -eq $true)
}
return $retrunItem
}
}
#######################
<#
.SYNOPSIS
Will get the Calendar WunderBar ShortCut for the Default Calendar Folder in a Mailbox utilizing the Alternate Net Companies API

.DESCRIPTION
Will get the Calendar WunderBar ShortCut for the Default Calendar Folder utilizing the Alternate Net Companies API

Requires the EWS Managed API from https://www.microsoft.com/en-us/download/particulars.aspx?id=42951

.EXAMPLE
Instance 1 To create a Folder named take a look at within the Root of the Mailbox
Get-DefaultCalendarFolderShortCut -Mailboxname [email protected]
#>
########################
perform Get-DefaultCalendarFolderShortCut{
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=1, Obligatory=$true)] [System.Management.Automation.PSCredential]$Credentials,
[Parameter(Place=2, Obligatory=$false)] [switch]$useImpersonation
)
Start
{
$service = Join-Alternate -MailboxName $MailboxName -Credentials $Credentials
if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
}
$PidTagWlinkGroupName = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6851, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
$PidTagWlinkCalendarColor = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6853, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
$ShortCutItems = Exec-FindCalendarShortCut -MailboxName $MailboxName -service $service
Write-Output ("Number of ShortCuts Founds " + $ShortCutItems.Depend)
foreach($Merchandise in $ShortCutItems){
$GroupNameVal = $null
$CalendarColor = $null
[Void]$Merchandise.TryGetProperty($PidTagWlinkCalendarColor,[ref]$CalendarColor)
if($Merchandise.TryGetProperty($PidTagWlinkGroupName,[ref]$GroupNameVal))
{
Write-Host ("Group Name " + $GroupNameVal + " Color " + [CalendarColor]$CalendarColor)
Write-Output $Merchandise
}
}



}
}
#######################
<#
.SYNOPSIS
Units the Colour of the Calendar WunderBar ShortCut for the Default Calendar Folder in a Mailbox utilizing the Alternate Net Companies API

.DESCRIPTION
Units the Colour of the Calendar WunderBar ShortCut for the Default Calendar Folder utilizing the Alternate Net Companies API

Requires the EWS Managed API from https://www.microsoft.com/en-us/download/particulars.aspx?id=42951

.EXAMPLE
Instance 1 To create a Folder named take a look at within the Root of the Mailbox
Set-DefaultCalendarFolderShortCut -Mailboxname [email protected] -CalendarColor Pink

#>
########################
perform Set-DefaultCalendarFolderShortCut{
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=1, Obligatory=$true)] [System.Management.Automation.PSCredential]$Credentials,
[Parameter(Place=2, Obligatory=$false)] [switch]$useImpersonation,
[Parameter(Place=3, Obligatory=$true)] [CalendarColor]$CalendarColor
)
Start
{
$intVal = $CalendarColor -as [int]
$service = Join-Alternate -MailboxName $MailboxName -Credentials $Credentials
$PidTagWlinkGroupName = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6851, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
$PidTagWlinkCalendarColor = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6853, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
}
$ShortCutItems = Exec-FindCalendarShortCut -MailboxName $MailboxName -service $service
Write-Output ("Number of ShortCuts Founds " + $ShortCutItems.Depend)
foreach($Merchandise in $ShortCutItems){
$GroupNameVal = $null
$CalendarColorValue = $null
[Void]$Merchandise.TryGetProperty($PidTagWlinkCalendarColor,[ref]$CalendarColorValue)
if($Merchandise.TryGetProperty($PidTagWlinkGroupName,[ref]$GroupNameVal))
{
Write-Host ("Group Name " + $GroupNameVal + " Current Color " + [CalendarColor]$CalendarColorValue)
$Merchandise.SetExtendedProperty($PidTagWlinkCalendarColor,$intVal)
$Merchandise.Replace([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
}
}



}
}
#######################
<#
.SYNOPSIS
Will get the Calendar WunderBar ShortCut for a Shared Calendar Folder in a Mailbox utilizing the Alternate Net Companies API

.DESCRIPTION
Will get the Calendar WunderBar ShortCut for a Shared Calendar Folder utilizing the Alternate Net Companies API

Requires the EWS Managed API from https://www.microsoft.com/en-us/download/particulars.aspx?id=42951

.EXAMPLE
Instance 1 To create a Folder named take a look at within the Root of the Mailbox
Get-DefaultCalendarFolderShortCut -Mailboxname [email protected] -$SharedCalendarMailboxName [email protected]
#>
########################
perform Get-SharedCalendarFolderShortCut{
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=1, Obligatory=$true)] [System.Management.Automation.PSCredential]$Credentials,
[Parameter(Place=2, Obligatory=$false)] [switch]$useImpersonation,
[Parameter(Place=3, Obligatory=$true)] [string]$SharedCalendarMailboxName
)
Start
{
$service = Join-Alternate -MailboxName $MailboxName -Credentials $Credentials

if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
}
$PidTagWlinkAddressBookEID = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6854,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkFolderType = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x684F, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkCalendarColor = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6853, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
$PidTagWlinkGroupName = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6851, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
$CommonViewFolder = Get-CommonViews -MailboxName $MailboxName -service $service

Write-Host ("Getting Autodiscover Settings Target")
Write-Host ("Getting Autodiscover Settings Mailbox")
$adset = GetAutoDiscoverSettings -adEmailAddress $MailboxName -Credentials $Credentials
$storeID = ""
if($adset -is [Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverResponse]){
Write-Host ("Get StoreId")
$storeID = GetStoreId -AutoDiscoverSettings $adset
}
$adset = $null
$abTargetABEntryId = ""
$adset = GetAutoDiscoverSettings -adEmailAddress $SharedCalendarMailboxName -Credentials $Credentials
if($adset -is [Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverResponse]){
Write-Host ("Get AB Id")
$abTargetABEntryId = GetAddressBookId -AutoDiscoverSettings $adset
$SharedUserDisplayName = $adset.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDisplayName]
}
$ExistingShortCut = $false
$psPropset= new-object Microsoft.Alternate.WebServices.Knowledge.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropset.add($PidTagWlinkAddressBookEID)
$psPropset.add($PidTagWlinkFolderType)
$psPropset.add($PidTagWlinkCalendarColor)
$psPropset.add($PidTagWlinkGroupName)
$ivItemView = New-Object Microsoft.Alternate.WebServices.Knowledge.ItemView(1000)
$ivItemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Related
$ivItemView.PropertySet = $psPropset
$fiItems = $service.FindItems($CommonViewFolder.Id,$ivItemView)
foreach($Merchandise in $fiItems.Objects){
$aeidVal = $null
$CalendarColorValue = $null
$GroupNameVal = $null
[Void]$Merchandise.TryGetProperty($PidTagWlinkCalendarColor,[ref]$CalendarColorValue)
[Void]$Merchandise.TryGetProperty($PidTagWlinkGroupName,[ref]$GroupNameVal)
if($Merchandise.TryGetProperty($PidTagWlinkAddressBookEID,[ref]$aeidVal)){
$fldType = $null
if($Merchandise.TryGetProperty($PidTagWlinkFolderType,[ref]$fldType)){
if([System.BitConverter]::ToString($fldType).Exchange("-","") -eq "0278060000000000C000000000000046"){
if([System.BitConverter]::ToString($aeidVal).Exchange("-","") -eq $abTargetABEntryId){
Write-Host ("Group Name " + $GroupNameVal + " Current Color " + [CalendarColor]$CalendarColorValue)
Write-Output $Merchandise
}
}
}
}
}



}
}
#######################
<#
.SYNOPSIS
Units the Colour of the Shared Calendar WunderBar ShortCut for the Shared Calendar Folder Shortcut in a Mailbox utilizing the Alternate Net Companies API

.DESCRIPTION
Units the Colour of the Shared Calendar WunderBar ShortCut for the Shared Calendar Folder Shortcut in a Mailbox utilizing the Alternate Net Companies API

Requires the EWS Managed API from https://www.microsoft.com/en-us/download/particulars.aspx?id=42951

.EXAMPLE
Instance 1 To create a Folder named take a look at within the Root of the Mailbox
Set-DefaultCalendarFolderShortCut -Mailboxname [email protected] -$SharedCalendarMailboxName [email protected] -CalendarColor Pink

#>
########################
perform Set-SharedCalendarFolderShortCut{
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=1, Obligatory=$true)] [System.Management.Automation.PSCredential]$Credentials,
[Parameter(Place=2, Obligatory=$false)] [switch]$useImpersonation,
[Parameter(Place=3, Obligatory=$true)] [string]$SharedCalendarMailboxName,
[Parameter(Place=4, Obligatory=$true)] [CalendarColor]$CalendarColor
)
Start
{
$service = Join-Alternate -MailboxName $MailboxName -Credentials $Credentials
$intVal = $CalendarColor -as [int]
if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
}
$PidTagWlinkAddressBookEID = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6854,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkFolderType = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x684F, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkCalendarColor = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6853, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
$PidTagWlinkGroupName = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6851, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
$CommonViewFolder = Get-CommonViews -MailboxName $MailboxName -service $service

Write-Host ("Getting Autodiscover Settings Target")
Write-Host ("Getting Autodiscover Settings Mailbox")
$adset = GetAutoDiscoverSettings -adEmailAddress $MailboxName -Credentials $Credentials
$storeID = ""
if($adset -is [Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverResponse]){
Write-Host ("Get StoreId")
$storeID = GetStoreId -AutoDiscoverSettings $adset
}
$adset = $null
$abTargetABEntryId = ""
$adset = GetAutoDiscoverSettings -adEmailAddress $SharedCalendarMailboxName -Credentials $Credentials
if($adset -is [Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverResponse]){
Write-Host ("Get AB Id")
$abTargetABEntryId = GetAddressBookId -AutoDiscoverSettings $adset
$SharedUserDisplayName = $adset.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDisplayName]
}
$ExistingShortCut = $false
$psPropset= new-object Microsoft.Alternate.WebServices.Knowledge.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropset.add($PidTagWlinkAddressBookEID)
$psPropset.add($PidTagWlinkFolderType)
$psPropset.add($PidTagWlinkCalendarColor)
$psPropset.add($PidTagWlinkGroupName)
$ivItemView = New-Object Microsoft.Alternate.WebServices.Knowledge.ItemView(1000)
$ivItemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Related
$ivItemView.PropertySet = $psPropset
$fiItems = $service.FindItems($CommonViewFolder.Id,$ivItemView)
foreach($Merchandise in $fiItems.Objects){
$aeidVal = $null
if($Merchandise.TryGetProperty($PidTagWlinkAddressBookEID,[ref]$aeidVal)){
$fldType = $null
$CalendarColorValue = $null
$GroupNameVal = $null
[Void]$Merchandise.TryGetProperty($PidTagWlinkCalendarColor,[ref]$CalendarColorValue)
[Void]$Merchandise.TryGetProperty($PidTagWlinkGroupName,[ref]$GroupNameVal)
if($Merchandise.TryGetProperty($PidTagWlinkFolderType,[ref]$fldType)){
if([System.BitConverter]::ToString($fldType).Exchange("-","") -eq "0278060000000000C000000000000046"){
if([System.BitConverter]::ToString($aeidVal).Exchange("-","") -eq $abTargetABEntryId){
Write-Host ("Group Name " + $GroupNameVal + " Current Color " + [CalendarColor]$CalendarColorValue)
$Merchandise.SetExtendedProperty($PidTagWlinkCalendarColor,$intVal)
$Merchandise.Replace([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
}
}
}
}
}



}
}


perform GetAutoDiscoverSettings{
param (
$adEmailAddress = "$( throw 'emailaddress is a compulsory Parameter' )",
[System.Management.Automation.PSCredential]$Credentials = "$( throw 'Credentials is a compulsory Parameter' )"
)
course of{
$adService = New-Object Microsoft.Alternate.WebServices.AutoDiscover.AutodiscoverService($ExchangeVersion);
$adService.Credentials = New-Object System.Internet.NetworkCredential($Credentials.UserName.ToString(),$Credentials.GetNetworkCredential().password.ToString())
$adService.EnableScpLookup = $false;
$adService.RedirectionUrlValidationCallback = {$true}
$UserSettings = new-object Microsoft.Alternate.WebServices.Autodiscover.UserSettingName[] 3
$UserSettings[0] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDN
$UserSettings[1] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::InternalRpcClientServer
$UserSettings[2] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDisplayName
$adResponse = $adService.GetUserSettings($adEmailAddress , $UserSettings);
return $adResponse
}
}
perform GetAddressBookId{
param (
$AutoDiscoverSettings = "$( throw 'AutoDiscoverSettings is a compulsory Parameter' )"
)
course of{
$userdnString = $AutoDiscoverSettings.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDN]
$userdnHexChar = $userdnString.ToCharArray();
foreach ($component in $userdnHexChar) {$userdnStringHex = $userdnStringHex + [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($component))}
$Supplier = "00000000DCA740C8C042101AB4B908002B2FE1820100000000000000"
$userdnStringHex = $Supplier + $userdnStringHex + "00"
return $userdnStringHex
}
}
perform GetStoreId{
param (
$AutoDiscoverSettings = "$( throw 'AutoDiscoverSettings is a compulsory Parameter' )"
)
course of{
$userdnString = $AutoDiscoverSettings.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDN]
$userdnHexChar = $userdnString.ToCharArray();
foreach ($component in $userdnHexChar) {$userdnStringHex = $userdnStringHex + [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($component))}
$serverNameString = $AutoDiscoverSettings.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::InternalRpcClientServer]
$serverNameHexChar = $serverNameString.ToCharArray();
foreach ($component in $serverNameHexChar) {$serverNameStringHex = $serverNameStringHex + [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($component))}
$flags = "00000000"
$ProviderUID = "38A1BB1005E5101AA1BB08002B2A56C2"
$versionFlag = "0000"
$DLLFileName = "454D534D44422E444C4C00000000"
$WrappedFlags = "00000000"
$WrappedProviderUID = "1B55FA20AA6611CD9BC800AA002FC45A"
$WrappedType = "0C000000"
$StoredIdStringHex = $flags + $ProviderUID + $versionFlag + $DLLFileName + $WrappedFlags + $WrappedProviderUID + $WrappedType + $serverNameStringHex + "00" + $userdnStringHex + "00"
return $StoredIdStringHex
}
}
perform hex2binarray($hexString){
$i = 0
[byte[]]$binarray = @()
whereas($i -le $hexString.size - 2){
$strHexBit = ($hexString.substring($i,2))
$binarray += [byte]([Convert]::ToInt32($strHexBit,16))
$i = $i + 2
}
return ,$binarray
}
perform ConvertId($EWSid){
$aiItem = New-Object Microsoft.Alternate.WebServices.Knowledge.AlternateId
$aiItem.Mailbox = $MailboxName
$aiItem.UniqueId = $EWSid
$aiItem.Format = [Microsoft.Exchange.WebServices.Data.IdFormat]::EWSId;
return $service.ConvertId($aiItem, [Microsoft.Exchange.WebServices.Data.IdFormat]::StoreId)
}
perform LoadProps()
{

}
perform Create-SharedCalendarShortCut
{
param(
[Parameter(Place=0, Obligatory=$true)] [string]$SourceMailbox,
[Parameter(Place=1, Obligatory=$true)] [System.Management.Automation.PSCredential]$Credentials,
[Parameter(Place=2, Obligatory=$false)] [switch]$useImpersonation,
[Parameter(Place=3, Obligatory=$true)] [String]$TargetMailbox,
[Parameter(Place=4, Obligatory=$true)] [CalendarColor]$CalendarColor
)
Start
{
$intVal = $CalendarColor -as [int]
$service = Join-Alternate -MailboxName $SourceMailbox -Credentials $Credentials

if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $SourceMailbox)
}
$pidTagStoreEntryId = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(4091, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagNormalizedSubject = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x0E1D,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);
$PidTagWlinkType = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6849, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
$PidTagWlinkFlags = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x684A, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
$PidTagWlinkOrdinal = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x684B, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkFolderType = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x684F, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkSection = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6852, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
$PidTagWlinkGroupHeaderID = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6842, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkSaveStamp = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6847, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
$PidTagWlinkGroupName = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6851, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
$PidTagWlinkStoreEntryId = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x684E, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkGroupClsid = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6850, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkEntryId = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x684C, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkRecordKey = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x684D, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkCalendarColor = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6853, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
$PidTagWlinkAddressBookEID = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6854,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkROGroupType = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6892,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
$PidTagWlinkAddressBookStoreEID = new-object Microsoft.Alternate.WebServices.Knowledge.ExtendedPropertyDefinition(0x6891,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$CommonViewFolder = Get-CommonViews -MailboxName $SourceMailbox -service $service
#Get the TargetUsers Calendar
# Bind to the Calendar Folder
$fldPropset = new-object Microsoft.Alternate.WebServices.Knowledge.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$fldPropset.Add($pidTagStoreEntryId);
if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $TargetMailbox)
}
$folderid= new-object Microsoft.Alternate.WebServices.Knowledge.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$TargetMailbox)
$TargetCalendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid,$fldPropset)
#Verify for current ShortCut for TargetMailbox
#Get AddressBook Id for TargetUser
Write-Host ("Getting Autodiscover Settings Target")
Write-Host ("Getting Autodiscover Settings Mailbox")
$adset = GetAutoDiscoverSettings -adEmailAddress $SourceMailbox -Credentials $Credentials
$storeID = ""
if($adset -is [Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverResponse]){
Write-Host ("Get StoreId")
$storeID = GetStoreId -AutoDiscoverSettings $adset
}
$adset = $null
$abTargetABEntryId = ""
$adset = GetAutoDiscoverSettings -adEmailAddress $TargetMailbox -Credentials $Credentials
if($adset -is [Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverResponse]){
Write-Host ("Get AB Id")
$abTargetABEntryId = GetAddressBookId -AutoDiscoverSettings $adset
$SharedUserDisplayName = $adset.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::UserDisplayName]
}
$ExistingShortCut = $false
$psPropset= new-object Microsoft.Alternate.WebServices.Knowledge.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropset.add($PidTagWlinkAddressBookEID)
$psPropset.add($PidTagWlinkFolderType)
$ivItemView = New-Object Microsoft.Alternate.WebServices.Knowledge.ItemView(1000)
$ivItemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Related
$ivItemView.PropertySet = $psPropset
$fiItems = $service.FindItems($CommonViewFolder.Id,$ivItemView)
foreach($Merchandise in $fiItems.Objects){
$aeidVal = $null
if($Merchandise.TryGetProperty($PidTagWlinkAddressBookEID,[ref]$aeidVal)){
$fldType = $null
if($Merchandise.TryGetProperty($PidTagWlinkFolderType,[ref]$fldType)){
if([System.BitConverter]::ToString($fldType).Exchange("-","") -eq "0278060000000000C000000000000046"){
if([System.BitConverter]::ToString($aeidVal).Exchange("-","") -eq $abTargetABEntryId){
$ExistingShortCut = $true
Write-Host "Found existing Shortcut"
###$Merchandise.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete)
}
}
}
}
}
if($ExistingShortCut -eq $false){
$objWunderBarLink = New-Object Microsoft.Alternate.WebServices.Knowledge.EmailMessage -ArgumentList $service
$objWunderBarLink.Topic = $SharedUserDisplayName
$objWunderBarLink.ItemClass = "IPM.Microsoft.WunderBar.Link"
$objWunderBarLink.SetExtendedProperty($PidTagWlinkAddressBookEID,(hex2binarray $abTargetABEntryId))
$objWunderBarLink.SetExtendedProperty($PidTagWlinkAddressBookStoreEID,(hex2binarray $storeID))
$objWunderBarLink.SetExtendedProperty($PidTagWlinkCalendarColor,$intVal)
$objWunderBarLink.SetExtendedProperty($PidTagWlinkFlags,0)
$objWunderBarLink.SetExtendedProperty($PidTagWlinkGroupName,"Shared Calendars")
$objWunderBarLink.SetExtendedProperty($PidTagWlinkFolderType,(hex2binarray "0278060000000000C000000000000046"))
$objWunderBarLink.SetExtendedProperty($PidTagWlinkGroupClsid,(hex2binarray "B9F0060000000000C000000000000046"))
$objWunderBarLink.SetExtendedProperty($PidTagWlinkROGroupType,-1)
$objWunderBarLink.SetExtendedProperty($PidTagWlinkSection,3)
$objWunderBarLink.SetExtendedProperty($PidTagWlinkType,2)
$objWunderBarLink.IsAssociated = $true
$objWunderBarLink.Save($CommonViewFolder.Id)
Write-Host ("ShortCut Created for - " + $SharedUserDisplayName)
}

}
}

Leave a Reply