Skip to content

Instantly share code, notes, and snippets.

@bill-long
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bill-long/1361e6a9aaa4d34ef488 to your computer and use it in GitHub Desktop.
Save bill-long/1361e6a9aaa4d34ef488 to your computer and use it in GitHub Desktop.
#################################################################################
# RemoveProperty.ps1
#
# Removes property 0x3FC9 from public folders.
param([string]$FolderPath, [string]$HostName, [string]$UserName, [boolean]$Recurse, [boolean]$Fix)
#################################################################################
# Update the path below to match the actual path to the EWS managed API DLL.
#
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
#
#################################################################################
#################################################################################
# Change this function to do whatever you want to do to each public folder
#
$propertyToDelete = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x3FC9, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
function DoFolder($folder, $path)
{
"Checking folder: " + $path
$propValue = $null
if ($folder.TryGetProperty($propertyToDelete, [ref]$propValue))
{
"Found property on folder: " + $path
if ($Fix)
{
$folder.RemoveExtendedProperty($propertyToDelete) | Out-Null
$folder.Update()
"Removed property from folder: " + $path
}
}
}
#
#################################################################################
$FolderPath = $FolderPath.Trim(@('\'))
if ($HostName -eq "")
{
$HostName = Read-Host "Hostname for EWS endpoint (leave blank to attempt Autodiscover)"
}
if ($UserName -eq "")
{
$UserName = Read-Host "User (UPN format)"
}
$password = $host.ui.PromptForCredential("Credentials", "Please enter your password to authenticate to EWS.", $UserName, "").GetNetworkCredential().Password
# If a URL was specified we'll use that; otherwise we'll use Autodiscover
$exchService = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$exchService.Credentials = new-object System.Net.NetworkCredential($UserName, $password, "")
if ($HostName -ne "")
{
("Using EWS URL " + "https://" + $HostName + "/EWS/Exchange.asmx")
$exchService.Url = new-object System.Uri(("https://" + $HostName + "/EWS/Exchange.asmx"))
}
else
{
("Autodiscovering " + $UserName + "...")
$exchService.AutoDiscoverUrl($UserName, {$true})
}
if ($exchService.Url -eq $null)
{
return
}
$pfsRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService, [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot)
if ($pfsRoot -eq $null)
{
return
}
$folder = $pfsRoot
if ($FolderPath.Length -gt 0)
{
$tinyView = new-object Microsoft.Exchange.WebServices.Data.FolderView(2)
$displayNameProperty = [Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName
$folderPathSplits = $FolderPath.Split(@('\'))
for ($x = 0; $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]
}
}
function DoSubfoldersRecursive($folder, $path)
{
$folderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(2147483647)
$displayName = [Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName
$arrayOfPropertiesToLoad = ($displayName, $propertyToDelete)
$folderView.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet($arrayOfPropertiesToLoad)
$subfolders = $folder.FindFolders($folderView)
foreach ($subfolder in $subfolders)
{
try
{
DoFolder $subfolder ($path + "\" + $subfolder.DisplayName)
DoSubfoldersRecursive $subfolder ($path + "\" + $subfolder.DisplayName)
}
catch { "Error processing folder: " + $subfolder.DisplayName }
}
}
if ($Recurse)
{
if ($FolderPath.Length -gt 0)
{
DoFolder $folder $FolderPath
}
DoSubfoldersRecursive $folder $FolderPath
}
else
{
DoFolder $folder $FolderPath
}
"Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment