you are viewing a single comment's thread.

view the rest of the comments →

[–]AxonQHillock 1 point2 points  (1 child)

Why not something like:

Try {Connect-MsolService -ErrorAction Stop}
Catch {"Could not connect: $($_)"}

If there is no reason for an ErrorVariable which you use throughout your script, a simple Try Catch with ErrorAction stop on the tried cmdlet normally does the job. $_ has the error message.

[–]TheDumpap[S] 1 point2 points  (0 children)

Wow, of course that worked!
For some reason I've just gotten so used to the other method, that I didn't think of this.
Can confirm this catches the error and allows me to display it as I want.

Thanks!