Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reproduce an error binding to a calendar public folder
#####
# Change as needed
$folderPath = "\Bill\Some Calendar"
$user = "bill@contoso.com"
#
#####
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
$password = $host.ui.PromptForCredential("Credentials", "Please enter your password to authenticate to EWS.", $user, "").GetNetworkCredential().Password
$exchService = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2016)
$exchService.Credentials = new-object System.Net.NetworkCredential($user, $password, "")
$exchService.AutoDiscoverUrl($user, {$true})
$pfsRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService, [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot)
$tinyView = new-object Microsoft.Exchange.WebServices.Data.FolderView(2)
$displayNameProperty = [Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName
# Find the folder
$tinyView = new-object Microsoft.Exchange.WebServices.Data.FolderView(2)
$displayNameProperty = [Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName
$folderPathSplits = $FolderPath.Split(@('\'))
$folder = $pfsRoot # Starting from the root
for ($x = 1; $x -lt $folderPathSplits.Length;$x++)
{
$filter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($displayNameProperty, $folderPathSplits[$x])
$results = $folder.FindFolders($filter, $tinyView)
if ($results.TotalCount -gt 1)
{
("Ambiguous name: " + $folderPathSplits[$x])
return
}
elseif ($results.TotalCount -lt 1)
{
("Folder not found: " + $folderPathSplits[$x])
return
}
$folder = $results.Folders[0]
}
# At this point, the result of the folder we specified is in $folder
# Now try to bind to that Id
[Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService, $folder.Id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.