[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$SMTPAddress,
[Parameter(Mandatory=$True)]
[string]$EmailErrorFrom,
[Parameter(Mandatory=$True)]
[string[]]$EmailErrorsTo,
[Parameter(Mandatory=$True)]
[string]$EmailSearchSubject,
[Parameter(Mandatory=$False)]
[string]$SMTPServer = "mailhost.susq.com",
[Parameter(Mandatory=$false)]
[string]$EwsUrl,
#[string]$EwsUrl = "https://webmail.susq.com/EWS/Exchange.asmx" ,
[Parameter(Mandatory=$false)]
[ValidateSet("Exchange2007","Exchange2007_SP1","Exchange2007_SP2","Exchange2007_SP3","Exchange2010","Exchange2010_SP1","Exchange2010_SP2","Exchange2010_SP3")]
[string]$ExchangeVersion = "Exchange2010_SP2",
[Parameter(Mandatory=$false)]
[string]$EWSManagedApiPath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
)
Write-Verbose "Ignoring any SSL certificate errors"
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true };
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}
[void][Reflection.Assembly]::LoadFile($EWSManagedApiPath)
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$SMTPAddress)
Try {$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($EWSservice,$folderid)}
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>$SMTPAddress</B>" -HaltScript}
If ($EWSURL)
{
Write-Verbose "Using the specifed EWS URL of $EWSURL"
$EWSService.URL = New-Object Uri($EWSURL)
}
Else
{
Write-Verbose "Using the AutoDiscover to find the EWS URL"
$EWSService.AutodiscoverUrl($SMTPAddress, {$True})
}
$query = "Subject:$EmailSearchSubject AND Received:today"
$FoundEmails = $EWSservice.FindItems("Inbox",$query,$view)
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
send-mailmessage -from $EmailErrorFrom -to $EmailErrorsTo -smtpserver $SMTPServer -subject $Subject -Body ($body | Out-String) -BodyAsHtml
}
If ($HaltScript) {Exit 1}
}
Function EumItem {
$NumEmailsToReturn = $InboxFolder.TotalCount
If ($InboxFolder.TotalCount -eq 0)
{
Write-Warning "Inbox is empty, exiting script or the Runtime Account of $($ENV:USERNAME) does not have impersonation rights for $SMTPAddress"
Send-ErrorReport -Subject "Inbox is empty" -body "Inbox is empty, exiting script or the Runtime Account of $($ENV:USERNAME) does not have rights for $SMTPAddress" -HaltScript
}
$ITEMView = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ItemView($NumEmailsToReturn)
$propertyset = New-Object Microsoft.Exchange.WebServices.Data.PropertySet ([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$view.PropertySet = $propertyset
}
Function EumFolder {
[CmdletBinding()]
Param(
[Parameter()]
$SearchScope, #Should be the type as same as [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox
[Parameter()]
[String]$SubFolderName
)
$SubFolderid=$null
$FolderView = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ItemView(100)
$FoundFolders = $EWSservice.FindFolders($SearchScope,$FolderView)
foreach ($subfolder in $FoundFolders.Folders)
{
If ($subfolder.DisplayName -eq $SubFolderName)
{
$SubFolderid= $subfolder.id
Break
}
}
return $SubFolderid
}