VS代码调用Get-Mailbox中的PS Try-catch不会捕获错误

时间:2017-08-09 14:17:20

标签: powershell try-catch visual-studio-code office365

我有一个功能,列出Office365中指定用户的所有电子邮件地址,我使用此功能检查电子邮件地址是否可用。

为什么Try-Catch在VS Code中不起作用,但在PS ISE中起作用?

Function ListAllEmailAdresses
{
  param ($ID)
  Try
  {
    $Mailbox = Get-Mailbox -Identity $ID -ErrorAction Stop
    $VarUser = Get-User -Identity $ID -ErrorAction Stop
    Write-Output "Email is occupied by: $Mailbox, $($VarUser.Title)"
    Write-Output $Mailbox.EmailAddresses
  }
  Catch
  {
    Write-Output "E-mail $ID is available"
  }
}

在VS Code中调用此函数时,Try-Catch无效。

PS C:\script> ListAllEmailAdresses -ID 'lidingo@elon.se'
The operation couldn't be performed because object 'lidingo@elon.se' couldn't be found on 'VI1PR03DC0025.eurprd03.prod.outlook.com'.
+ CategoryInfo          : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
+ FullyQualifiedErrorId : [Server=VI1PR03MB3165,RequestId=62016320-c687-41ed-9949-4b0fe854d821,TimeStamp=2017-08-09 13:54:40] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 2CD51ECE,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
+ PSComputerName        : ps.outlook.com

The operation couldn't be performed because object 'lidingo@elon.se' couldn't be found on 'VI1PR03DC0025.eurprd03.prod.outlook.com'.
+ CategoryInfo          : NotSpecified: (:) [Get-User], ManagementObjectNotFoundException
+ FullyQualifiedErrorId : [Server=VI1PR03MB3165,RequestId=db435869-8b64-4edf-bb90-a2eb836b07fa,TimeStamp=2017-08-09 13:54:41] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] DAFB12B9,Microsoft.Exchange.Management.RecipientTasks.GetUser
+ PSComputerName        : ps.outlook.com

Email is occupied by: ,

PS错误说无法找到被叫E-mailadrdress,因此它应该到达Catch-code。但它没有:(

如果我在Powershell ISE中调用相同的函数,我得到这个结果:

PS C:\Script> ListAllEmailAdresses -ID 'lidingo@elon.se'
E-mail lidingo@elon.se is available

PS错误被捕获,我得到了想要的输出。

/安德斯

0 个答案:

没有答案
相关问题