0

This PS 3.0 code was working just fine but suddenly is throwing an exception. I don't understand which method is this referring to.

param($mailboxName = "[email protected]",
$smtpServerName = "a.NET",
$emailFrom = "[email protected]",
$emailTo = "[email protected]"
)
Clear-Host 
Set-ExecutionPolicy RemoteSigned 
Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"

try 
{
  $Exchange2007SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1
  $Exchange2010    = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010
  $Exchange2010SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
  $Exchange2010SP2 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
  $Exchange2013    = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013
  $Exchange2013SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1

  $exchangeService = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList $Exchange2010SP2
  $exchangeService.UseDefaultCredentials = $true
  $exchangeService.AutodiscoverUrl($mailboxName)

 $inboxFolderName = New-object Microsoft.Exchange.WebServices.Data.FolderId ([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
  $inboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchangeService,$inboxFolderName)
  $Sfha = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo ([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true)
  $sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection    ([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);

  $ItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(10)

$downloadDir = "D:\Files"   
  $Results = $inboxFolder.FindItems($sfha,$ItemView)
  $today = Get-Date -Format "dd-MM-yyyy"
  #$Results
  cd $downloadDir
  mkdir $today

  foreach ($Res in $Results.Items)
  {
    $Res
    $Res.Load()
    $fiFile = new-object System.IO.FileStream(($downloadDir + “\” + $today + "\" + $attach.Name.ToString()),        [System.IO.FileMode]::Create)

    foreach ($attach in $Res.Attachments)
    {
        $attach.Load()        
        $fiFile.Write($attach.Content, 0, $attach.Content.Length)
    write-host "Downloaded Attachment : " + (($downloadDir + “\” + $attach.Name.ToString()))
    }
    $fiFile.Close()     
    $Res.isread = $true
    $Res.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
 }

}catch
{
      echo $_.Exception | format-list -Force  
}

The error is:

ErrorRecord                 : You cannot call a method on a null-valued expression.
StackTrace                  :    at CallSite.Target(Closure , CallSite , Object )
                                 at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
                                 at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame)
                                 at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
                                 at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
WasThrownFromThrowStatement : False
Message                     : You cannot call a method on a null-valued expression.
Data                        : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException              : 
TargetSite                  : Void CheckActionPreference(System.Management.Automation.Language.FunctionContext, System.Exception)
HelpLink                    : 
Source                      : System.Management.Automation
HResult                     : -2146233087
4
  • 1
    The reason you can't see what method call it's referring to is that you've stripped out useful information by doing $_.Exception | format-list -Force. To see the original ErrorRecord, inspect $Error[0] or change the catch block to just throw Commented Feb 2, 2016 at 17:06
  • Thanks Matt. I was using the attachment outside the for loop. Now all's good. :) Commented Feb 2, 2016 at 20:05
  • Cool, post an answer! (it's perfectly alright to post and accept answers to your own question if you managed to solve it on your own) :) Commented Feb 2, 2016 at 20:33
  • @MathiasR.Jessen, why not post your answer as an answer, so others on SO do not have to spend time looking at questions that show as unanswered, but are answered? Commented Feb 3, 2016 at 0:55

1 Answer 1

0

I was using $fiFile outside the for loop while using the attachment ($attach). Moved the declaration of $fiFile inside and it worked fine. All as I put echo $Error[0] inside the catch block.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.