Hi Glen
it's work for me i have one more doubt that is how to bind all mailbox folder using EWS ,then try with Inbox ,Sentitems,Deleteitems it's working but my need is bind all mailbox folder without giving folder name Like Inbox,setitems,etc.. pls help
me
i use following code
## Get the Mailbox to Access from the 1st commandline argument
#$MailboxName = 'william@xlc.local'
#$Range = "07/01/2015..07/01/2015"
#$AQSString = "System.Message.DateReceived:" + $Range
## Load Managed API dll
Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
## Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010
## Create Exchange Service Object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials
#Credentials Option 1 using UPN for the windows Account
#$psCred = Get-Credential
#$creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
#$service.Credentials = $creds
#$service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials("superadmin@xlc.local","Password1234")
#Credentials Option 2
$service.UseDefaultCredentials = $true
## Choose to ignore any SSL Warning issues caused by Self Signed Certificates
## Code From http://poshcode.org/624
## Create a compilation environment
$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Provider.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
$TASource=@'
namespace Local.ToolkitExtensions.Net.CertificatePolicy{
public class TrustAll : System.Net.ICertificatePolicy {
public TrustAll() {
}
public bool CheckValidationResult(System.Net.ServicePoint sp,
System.Security.Cryptography.X509Certificates.X509Certificate cert,
System.Net.WebRequest req, int problem) {
return true;
}
}
}
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly
## We now create an instance of the TrustAll and attach it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
## end code from http://poshcode.org/624
## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use
#CAS URL Option 1 Autodiscover
#$service.AutodiscoverUrl($MailboxName,{$true})
#"Using CAS Server : " + $Service.url
#CAS URL Option 2 Hardcoded
$uri=[system.URI] "https://localhost/ews/exchange.asmx"
$service.Url = $uri
## Optional section for Exchange Impersonation
#$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
function Process-Mailbox{
param (
$SmtpAddress = "$( throw 'SMTPAddress is a mandatory Parameter' )"
)
process{
# Bind to the Inbox Folder
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox, $SmtpAddress)
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropset.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text
#Define ItemView to retrive just 1000 Items
$ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)
$fiItems = $null
$fiItems = $service.FindItems($Inbox.Id,$ivItemView)
If($fiItems -ne $null)
{
[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)
foreach($Item in $fiItems.Items){
$Attachment = ''
foreach($attach in $Item.Attachments){
If($Attachment) {
$Attachment=$Attachment + ";" + $attach.Name.ToString()
}
Else {
$Attachment=$attach.Name.ToString()
}
}
$rec = ''
foreach($r in $Item.ToRecipients){
If($rec) {
$rec=$rec + ";" + $r.ToString()
}
Else {
$rec=$r.ToString()
}
}
New-Object -TypeName PSObject -Property @{
Date= $Item.DateTimeReceived
Sender =$Item.sender
Recevier = $rec
MessageSubject =$Item.Subject
Messagebody =$Item.Body.Text
Attachment = $Attachment
}
}
}
}
}
$(Get-Mailbox | foreach-object {
$SmtpAddress = $_.WindowsEmailAddress.ToString()
if($service.url -eq $null){
$service.AutodiscoverUrl($SmtpAddress,{$true})
"Using CAS Server : " + $Service.url
}
Try{
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $SmtpAddress)
Process-Mailbox -SmtpAddress $SmtpAddress
}
catch{
# Write-host ("Error processing Mailbox : " + $SmtpAddress + $_.Exception.Message.ToString())
$Error.Clear()
}
})|select Date,Sender,recevier,Messagesubject,Messagebody,Attachment
Thanks
Manidurai
-
Edited by
manidurai
2 hours 55 minutes ago