-
Notifications
You must be signed in to change notification settings - Fork 1
/
Get-xJunkEmail.ps1
38 lines (32 loc) · 1.41 KB
/
Get-xJunkEmail.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function Get-xJunkEmail {
[outputtype('Microsoft.Exchange.WebServices.Data.EmailMessage')]
param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
$MailBox,
[Parameter()]
$ItemCount,
[Parameter(Mandatory)]
[System.Management.Automation.CredentialAttribute()]
[pscredential]
$Credential
)
begin {
Import-Module 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'
}
process {
$ExchangeService = [Microsoft.Exchange.WebServices.Data.ExchangeService]::new()
$ExchangeService.ImpersonatedUserId =[Microsoft.Exchange.WebServices.Data.ImpersonatedUserId]::new([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$MailBox)
$ExchangeService.Credentials = [System.Net.NetworkCredential]::new($Credential.UserName,$Credential.Password)
$ExchangeService.Url = "https://outlook.office365.com/EWS/Exchange.asmx"
if($PSBoundParameters.ContainsKey('ItemCount')) {
$View = [Microsoft.Exchange.WebServices.Data.ItemView]::new($ItemCount)
}
else {
$View = [Microsoft.Exchange.WebServices.Data.ItemView]::new(10)
}
$ExchangeService.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::JunkEmail,$View)
}
end {
}
}