0
votes

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

1
the script above - where is it?Karl Reid
The second parameter needs to be an PropertySet object and it look like you passing in a type. You need to post the full code your usingGlen Scales
Thank you Glen @glen. Here is my code:akurbanyan
@Glen Scales. Hi Glen, Please see my original question for errors I get running your script. I couldn't find how to respond to a Stack Overflow answer. thank you for your help.akurbanyan

1 Answers

0
votes

Your missing a FindItems Call all you don't need to use LoadProperitesFromItems because you making a call to load the MimeContent in your iteration. A basic fix would be

    $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(1000, $offset)
    $results = $null
    do{  
        $results = $rootfolder.FindItems($view)  
        foreach ($mail in $results.Items){  
            if ($mail -is [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()
            }
        }       
        $view.Offset += $result.Items.Count    
    }while($results.MoreAvailable -eq $true) 

This code will pretty much page through every item in the Inbox and download each message as an EML file.