Try-catch in VS Code call to Get-Mailbox don't catch error
See original GitHub issueFrom @AndersElonGroup on August 10, 2017 14:7
- VSCode Version: Code 1.14.2 (cb82febafda0c8c199b9201ad274e25d9a76874e, 2017-07-19T23:34:09.706Z)
- OS Version: Windows 10 1703 (OS-version 16232.1000)
- PS Version: 5.1.16232.1000
- Extensions: Extension|Author (truncated)|Version —|—|— PowerShell|ms-|1.4.1
When connecting to Office365 in Powershell and call the cmd Get-Mailbox, VS Code do not Catch the Error thrown if the object (the email-address) cannot be found. I use this method to check if requested email-addresses is available. If the email-address is available the -ErrorAction Stop should throw an Error and the Catch will just display a message that the adress is available. If the email-adress is occupied I get information about the accountowning the adress.
When running the code in Powershell ISE i get the expected output, Catching the error. When running the code in VS Code, the error is not caught.
Steps to Reproduce: VS Code
- Open O365 Session in VS Code
- call the function with a adress that do not exist in O365
- VS Code do not Catch the error thrown by Get-Mailbox or Get-User
PS ISE 4. Open O365 Session in PS ISE 5. call the function with a adress that do not exist in O365 6. PS ISE Catch the error thrown by Get-Mailbox or Get-User and show the Catch-output
Reproduces without extensions: Yes
`#Open O365 Session #Get your credentials $cred=Get-Credential #Connect to O365 Connect-MsolService -Credential $cred
#import O365 Session commands $session=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic –AllowRedirection Import-PSSession $session -AllowClobber`
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" } }
Output when called fromVS Code: 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: ,
Output when called from PS ISE: PS C:\Script> ListAllEmailAdresses -ID ‘lidingo@elon.se’ E-mail lidingo@elon.se is available
Copied from original issue: Microsoft/vscode#32228
Issue Analytics
- State:
- Created 6 years ago
- Comments:16 (3 by maintainers)
Top GitHub Comments
@AndersElonGroup this is normal and is due to implicit remoting, not the VSCode PowerShell extension. Connecting to Exchange Online via PowerShell works by implicit remoting, which means you’re executing the Exchange commands on a remote Exchange server, not locally. This causes some issues with error handling because powerShell sees the remote commands as successful even though an exception was returned from the command. You can work around this by temporarily setting your global
$ErrorActionPreference
variable to “Stop.”Thanks a lot for the help Matt! Closing this issue out now.