【问题标题】:Powershell - Exception untraceablePowershell - 无法追踪的异常
【发布时间】:2016-02-02 16:48:41
【问题描述】:

此 PS 3.0 代码运行良好,但突然抛出异常。我不明白这是指哪种方法。

param($mailboxName = "A@b.com",
$smtpServerName = "a.NET",
$emailFrom = "c@d.com",
$emailTo = "e@f.com"
)
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  
}

错误是:

ErrorRecord :您不能在空值表达式上调用方法。
StackTrace : 在 CallSite.Target(Closure , CallSite , Object )
                                 在 System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,Tret](CallSite 站点,T0 arg0)
                                 在 System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame 框架)
                                 在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame 框架)
                                 在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame 框架)
WasThrownFromThrowStatement:错误
消息:您不能在空值表达式上调用方法。
数据:{System.Management.Automation.Interpreter.InterpretedFrameInfo}
内部异常:
目标站点:无效 CheckActionPreference(System.Management.Automation.Language.FunctionContext,System.Exception)
帮助链接:
来源:System.Management.Automation
HResult : -2146233087

【问题讨论】:

  • 您看不到它所指的方法调用的原因是您通过执行$_.Exception | format-list -Force 删除了有用的信息。要查看原始错误记录,请检查 $Error[0] 或将 catch 块更改为 throw
  • 谢谢马特。我在 for 循环之外使用附件。现在一切都好。 :)
  • @MathiasR.Jessen,为什么不将您的答案作为答案发布,这样 SO 上的其他人就不必花时间查看显示为未回答但已回答的问题?

标签: powershell


【解决方案1】:

我在使用附件 ($attach) 时在 for 循环之外使用了 $fiFile。将 $fiFile 的声明移到里面,它工作正常。就像我将 echo $Error[0] 放在 catch 块中一样。

【讨论】:

    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2022-10-15
    • 2011-11-05
    相关资源
    最近更新 更多