Exception calling "LoadPropertiesForItems" with "2" argument(s): "Value cannot be null. Parameter name: items" At line:20 char:1 + $response = $service.LoadPropertiesForItems($results, [Microsoft.Exch ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentNullException
I run the script below and get this error. Only thing I changed in the script is mailbox Identity in line 11. I don't know much about EWS. Can anyone help?
add-pssnapin Microsoft.Exchange.Management.PowerShell.snapin
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind
$service.AutodiscoverUrl($aceuser.mail.ToString())
$MailboxName = get-mailbox -Identity [email protected]
$folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName.PrimarySmtpAddress.ToString())
$rootfolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folderidcnt)
$offset = 0;
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(10000, $offset)
$response = $service.LoadPropertiesForItems($results, [Microsoft.Exchange.WebServices.Data.PropertySet]::FirstClassProperties)
foreach ($mail in $results){
if ($mail.ToString() -eq "Microsoft.Exchange.WebServices.Data.EmailMessage") {
$mailSubject = $mail.Subject
$mailProps = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent)
$mail.Load($mailProps)
#TODO: clean up $mailSubject so it's filesystem friendly
$fStream = New-Object System.IO.FileStream("C:\Temp\$mailSubject.eml", [System.IO.FileMode]::Create)
$fStream.Write($mail.MimeContent.Content, 0, $mail.MimeContent.Content.Length)
$fStream.Close()
}
}
Thank you Glen. I have 3 emails the mailbox I try to export. Your script exports first email and throws this: New-Object : Exception calling ".ctor" with "2" argument(s): "Illegal characters in path." At line:26 char:28 + ... $fStream = New-Object System.IO.FileStream("C:\Temp\$mailSubject.eml ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "Write" with "3" argument(s): "Cannot access a closed file." At line:27 char:17 + ... $fStream.Write($mail.MimeContent.Content, 0, $mail.MimeCo ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ObjectDisposedException
New-Object : Exception calling ".ctor" with "2" argument(s): "The given path's format is not supported." At line:26 char:28 + ... $fStream = New-Object System.IO.FileStream("C:\Temp\$mailSubject.eml ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "Write" with "3" argument(s): "Cannot access a closed file." At line:27 char:17 + ... $fStream.Write($mail.MimeContent.Content, 0, $mail.MimeCo ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ObjectDisposedException
the script above
- where is it? – Karl Reid