question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Try-catch in VS Code call to Get-Mailbox don't catch error

See original GitHub issue

From @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

  1. Open O365 Session in VS Code
  2. call the function with a adress that do not exist in O365
  3. 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:closed
  • Created 6 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mattmcnabbcommented, Aug 15, 2017

@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.”

0reactions
daviwilcommented, Aug 24, 2017

Thanks a lot for the help Matt! Closing this issue out now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Try-catch in VS Code call to Get-Mailbox don't catch error
I use this method to check if requested email-addresses is available. If the email-address is available the -ErrorAction Stop should throw an ...
Read more >
PS Try-catch in VS Code call to Get-Mailbox don't catch error
I have a funktion to list all email-addresses on a specified user in Office365 and I use this function to check if an...
Read more >
try... catch... not firing
So I input an entry that doesn't exist in our GAL, so "catch" should fire immediately. I can even see that the code...
Read more >
Remote Sessions, Try{}Catch{}, ErrorActionPreference
I have tried setting $ErrorActionPreference = 'Stop' in various sections and think I am scope-creeping/not catching the Error where I should be ...
Read more >
Try-Catch not working with Get-ADObject : r/PowerShell
I am guessing the catch command will not execute when there is no real "error" like in the Get-Mailbox command when finding no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found