You are on page 1of 3

# Powershell script to obtain the GOID/UID for appointment based on subject.

# This script creates a CSV file with results in the path specified.
#
###################################################################################
###########
#
# This script is not officially supported by Microsoft, use it at your own risk.
# Microsoft has no liability, obligations, warranty, or responsibility regarding
# any result produced by use of this file.
#
###################################################################################
###########
#
# Syntax for running this script:
#
# .\Find-CalendarItems.ps1 -Mailbox <SMTP address> -Subject <Subject> -OutputPath
<outputPath>
#
# Example:
#
# .\Find-CalendarItems.pst -Mailbox jim@contoso.com -Subject meetingSubject
-OutputPath c:\Temp
#
###################################################################################
###########
#
param(
[Parameter(Mandatory = $true)] [string] $Mailbox = "",
[Parameter(Mandatory = $true)] [string] $Subject = "",
[Parameter(Mandatory = $false)] [string] $OutputPath = ""
)
#Clear-Host

# Create a table to store the appointment data


$tableAppt = New-Object System.Data.DataTable
$col1 = $tableAppt.Columns.Add("Subject", [string])
$col2 = $tableAppt.Columns.Add("UID", [string])

## Load Managed API dll - thanks to Glen Scales for this


http://gsexdev.blogspot.com/
###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_MACHINE\SOFTWARE\Microsoft\Exchange\Web Services'|Sort-Object
Name -Descending| Select-Object -First 1 -ExpandProperty Name)).'Install
Directory') + "Microsoft.Exchange.WebServices.dll")
if (Test-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
}

# Set Exchange Version


$exchangeVersion =
[Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2

## Create Exchange Service Object


$service = New-Object
Microsoft.Exchange.WebServices.Data.ExchangeService($exchangeVersion)
$service.UserAgent = "FindCalendarItemScript"

# Get the credentials to connect to EWS


$psCred = Get-Credential
$creds = New-Object System.Net.NetworkCredential($psCred.UserName,
$psCred.GetNetworkCredential().Password) # http://msdn.microsoft.com/en-
us/library/system.net.networkcredential(v=vs.110).aspx
$service.Credentials = $creds

# Locate EWS URL using Autodiscover - http://msdn.microsoft.com/en-


us/library/office/dd633692(v=exchg.80).aspx
$service.AutodiscoverUrl($Mailbox, {$true}) # need
AutodiscoverRedirectionUrlValidationCallback for Office 365

# Connect to the Calendar for this mailbox


$calendarId= New-Object
Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.W
ellKnownFolderName]::Calendar,$Mailbox)
$folderId = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,
$calendarId)

# Set the search criteria


$searchFilter = "System.Subject:$Subject"

# Set the view for the FindItems


$viewBase = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)

# Search for the appointments based on subject


do{
$searchResults = $service.FindItems($folderId.Id,$searchFilter,$viewBase) #
.FindItems(FolderId, SearchFilter, ViewBase)
# Loop thru the search results if found to capture the GOID/UID
if($searchResults.Count -gt 0) {
foreach($results in $searchResults.Items){
$row = $tableAppt.NewRow()
$row.Subject = $results.Subject
$row.UID = $results.ICalUid
$tableAppt.Rows.Add($row)
}
$viewBase.Offset += $searchResults.Items.Count
}
else {
Write-Host "No items were found for the subject: $Subject" -ForegroundColor
Yellow
}
}
while($searchResults.MoreAvailable -eq $true)
#Write the table to a CSV when OutputPath is provided
if($OutputPath -ne "") {
$tableAppt | Export-Csv $OutputPath\uid.csv -NoTypeInformation
}
$tableAppt | fl

You might also like