[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$MailSMTPAddress,
[Parameter(Mandatory=$True)]
[string]$EmailErrorFrom,
[Parameter(Mandatory=$True)]
[string[]]$EmailErrorsTo,
[Parameter(Mandatory=$True)]
[string]$EmailSearchSubject,
[Parameter(Mandatory=$False)]
[string]$SMTPServer = "127.0.0.1",
[Parameter(Mandatory=$false)]
[string]$EwsUrl,
[Parameter(Mandatory=$false)]
[ValidateSet("Exchange2007","Exchange2007_SP1","Exchange2007_SP2","Exchange2007_SP3","Exchange2010","Exchange2010_SP1","Exchange2010_SP2","Exchange2010_SP3")]
[string]$ExchangeVersion = "Exchange2010_SP3",
[Parameter(Mandatory=$false)]
#[string]$EWSManagedApiPath = $(Split-Path -Parent $MyInvocation.MyCommand.Definition) + "\API\Microsoft.Exchange.WebServices.dll",
[string]$EWSManagedApiPath = "C:\Temp\API\Microsoft.Exchange.WebServices.dll",
[Parameter(Mandatory=$True)]
[string]$MailSearchFolder #Fomat should be "path\folder\"
)
Write-Host $($ENV:USERNAME)
Function Send-ErrorReport
{
[CmdletBinding()]
Param(
[Parameter(Position=0,Mandatory=$true)]
[string]$Subject,
[Parameter(Position=1,Mandatory=$true)]
[string]$body,
[Parameter()]
[switch]$HaltScript
)
Write-Warning $Subject
#Appened Script name to email Subject
If ($EmailFrom -and $EmailErrorsTo -and $SMTPServer)
{
$Subject = "Get-AttachmentFromEmail.ps1 : " + $Subject
Write-Host "error"
#send-mailmessage -from $EmailErrorFrom -to $EmailErrorsTo -smtpserver $SMTPServer -subject $Subject -Body ($body | Out-String) -BodyAsHtml
}
If ($HaltScript) {Exit 1}
} #end Function
Write-Verbose "Ignoring any SSL certificate errors"
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true };
#$mailbox = Get-Mailbox %mailboxname%
#$mailAddress = $mailbox.PrimarySmtpAddress.ToString();
Try {Get-Item -Path $EWSManagedApiPath -ErrorAction Stop | Out-Null}
Catch {Send-ErrorReport -Subject "EWS Managed API path cannot be accessed" -body "Please verify the following EWS API path on <B>$($ENV:COMPUTERNAME)</B> : <B>$EWSManagedApiPath</B>" -HaltScript}
[Reflection.Assembly]::LoadFile($EWSManagedApiPath) | Out-Null
$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::$ExchangeVersion)
If ($EWSURL)
{
Write-Verbose "Using the specifed EWS URL of $EWSURL"
$s.URL = New-Object Uri($EWSURL)
}
Else
{
Write-Verbose "Using the AutoDiscover to find the EWS URL"
$s.AutodiscoverUrl($MailSMTPAddress, {$True})
}
$ItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(10000)
$MailboxRootid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailSMTPAddress)
Try {$MailboxRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,$MailboxRootid)}
Catch {Send-ErrorReport -Subject "Account is not permissioned for accessing" -body "Please verify that the following account <B>$($ENV:USERNAME)</B> has the ability to access <B>$MailSMTPAddress</B>" -HaltScript}
# Get Folder ID from Path
Function GetFolder
{
[CmdletBinding()]
Param(
[Parameter(Position=0,Mandatory=$true)]
$RootFolder,
[Parameter(Position=1,Mandatory=$true)]
$FolderPath
)
# Return a reference to a folder specified by path
#$RootFolder, $FolderPath = $args[0];
$Folder = $RootFolder;
Write-Host $Folder + "/" + $FolderPath
if ($FolderPath -ne ‘\‘)
{
$PathElements = $FolderPath -split ‘\\‘;
For ($i=0; $i -lt $PathElements.Count; $i++)
{
if ($PathElements[$i])
{
$View = New-Object Microsoft.Exchange.WebServices.Data.FolderView(2,0);
$View.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;
$View.PropertySet = [Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly;
$SearchFilter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName, $PathElements[$i]);
$FolderResults = $Folder.FindFolders($SearchFilter, $View);
if ($FolderResults.TotalCount -ne 1)
{
# We have either none or more than one folder returned... Either way, we can‘t continue
$Folder = $null;
Write-Host "Failed to find " $PathElements[$i];
Send-ErrorReport -Subject "Failed to find " $PathElements[$i] -body "Please verify that the following folder <B>$PathElements[$i]</B> is under <B>$FolderPath</B>" -HaltScript
#Write-Host "Requested folder path: " $FolderPath;
break;
}
$Folder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s, $FolderResults.Folders[0].Id)
}
}
}
$Folder;
} #end Function
$FolderObject = GetFolder($MailboxRoot, $MailSearchFolder)
$propertyset = New-Object Microsoft.Exchange.WebServices.Data.PropertySet ([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$propertyset.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;
$View.PropertySet = $propertyset
$query = "Subject:$EmailSubject AND Received:today"
$findItemResults = $FolderObject.FindItems($query,$ItemView)
$iCount=0
foreach ($item in $findItemResults.Items)
{
$iCount++
$item.Load($propertyset);
$Console.WriteLine($item.Body.text);
}
#try {
# $FolderObject = GetFolder($MailboxRoot, "path\folder\");
#Date from and To
# $findItemResults = $FolderObject.FindItems("System.Message.DateReceived:01/01/2014..01/08/2014",$ItemView)
# foreach ($item in $findItemResults.Items) {
# try {
#Comment Below out to not delete
# [void]$item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
# $Deleted ++
#Uncomment below to list before deleting
#Write-host $item.DateTimeReceived
# } catch {
# Write-warning "Unable to delete item, $($item.subject). $($Error[0].Exception.Message)"
# }
# }
# if ($Deleted -gt 0) { Write-host "$Deleted mail items deleted from the Inbox." }
#} catch {
# Write-warning "Could not connect to Inbox. $( $_.exception.message )"
#}