0
votes

Hello when I run the following piece of code.

$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)

function ConvertId{
param (
$OwaId = "$( throw 'OWAId is a mandatory Parameter' )"
)
process{
$aiItem = New-Object Microsoft.Exchange.WebServices.Data.AlternateId
$aiItem.Mailbox = $MailboxName
$aiItem.UniqueId = $OwaId
$aiItem.Format = [Microsoft.Exchange.WebServices.Data.IdFormat]::OwaId
$convertedId = $service.ConvertId($aiItem, [Microsoft.Exchange.WebServices.Data.IdFormat]::EwsId)
return $convertedId.UniqueId
}
}

Get Folder id from EMS and bind to the Folder in EWS

get-mailboxfolderstatistics $MailboxName | ForEach-Object {

$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId((Convertid $_.FolderId))     
$ewsFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
$ewsFolder  

}

I receive the following error. Should the method be different for Exchange 2013.

ForEach-Object : Exception calling "ConvertId" with "2" argument(s): "Data is corrupt." At D:\Scripts\SAVE SCRIPTS\Get-FoldeIDsr.ps1:89 char:44 + get-mailboxfolderstatistics $MailboxName | ForEach-Object { + ~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [ForEach-Object], MethodInvocationException + FullyQualifiedErrorId : ServiceResponseException,Microsoft.PowerShell.Commands.ForEachObjectCommand

1

1 Answers

1
votes

You need to URLEncode the FolderId you get from EMS as it may have illegal characters eg

Add-Type -AssemblyName System.Web        
$urlEncodedId = [System.Web.HttpUtility]::UrlEncode($_.FolderId.ToString())
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId((Convertid $urlEncodedId))  
$ewsFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)