Skip to content

Instantly share code, notes, and snippets.

@bill-long
Created May 30, 2014 15:33
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/45473823fd67b0791a5c to your computer and use it in GitHub Desktop.
Save bill-long/45473823fd67b0791a5c to your computer and use it in GitHub Desktop.
Check search criteria on To-Do Search folder
#########################################
# GenericDoAllMailboxFoldersScript.ps1
#
param([string]$HostName, [string]$UserName, [string]$Mailbox, [string]$InputFile)
#########################################
# 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.0\Microsoft.Exchange.WebServices.dll"
#
#########################################
if ($InputFile -eq "" -and $Mailbox -eq "")
{
$Mailbox = Read-Host "Mailbox to process"
}
elseif ($InputFile -ne "" -and $Mailbox -ne "")
{
"-Mailbox and -InputFile are mutually exclusive. Only one may be specified."
return
}
$mailboxList = new-object 'System.Collections.Generic.List[string]'
if ($InputFile -ne "")
{
$inputReader = new-object System.IO.StreamReader($InputFile)
while ($null -ne ($buffer = $inputReader.ReadLine()))
{
if ($buffer.Length -gt 0)
{
$mailboxList.Add($buffer)
}
}
}
else
{
$mailboxList.Add($Mailbox)
}
("Number of mailboxes to process: " + $mailboxList.Count.ToString())
if ($mailboxList.Count -lt 1)
{
return
}
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]::Exchange2010)
$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 " + $mailboxList[0] + "...")
$exchService.AutoDiscoverUrl($mailboxList[0], {$true})
}
if ($exchService.Url -eq $null)
{
return
}
#########################################
# Properties we care about
# This is not actually used in this generic script
#
$subjectProperty = [Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject
$itemClassProperty = [Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass
$dateTimeReceivedProperty = [Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived
$arrayOfPropertiesToLoad = @($subjectProperty, $itemClassProperty, $dateTimeReceivedProperty)
$propertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet($arrayOfPropertiesToLoad)
#
#########################################
function DoToDoSearchFolder($rootFolder)
{
$folderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(2147483647)
$subfolders = $rootFolder.FindFolders($folderView)
foreach ($subfolder in $subfolders)
{
try
{
if ($subfolder.DisplayName -eq "To-Do Search")
{
$tds = [Microsoft.Exchange.WebServices.Data.SearchFolder]::Bind($exchService, $subfolder.Id)
# Not sure what to do here yet
}
}
catch { "Error processing folder: " + $subfolder.DisplayName }
}
}
function DoMailbox($thisMailbox)
{
$rootFolderName = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root
$rootId = new-object Microsoft.Exchange.WebServices.Data.FolderId($rootFolderName, $mbx)
$rootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService, $rootId)
if ($rootFolder -eq $null)
{
("Error. Could not open mailbox: " + $emailAddress)
return
}
"Opened mailbox: " + $emailAddress
DoToDoSearchFolder $rootFolder ""
}
foreach ($emailAddress in $mailboxList)
{
$mbx = new-object Microsoft.Exchange.WebServices.Data.Mailbox($emailAddress)
DoMailbox($mbx)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment