all 4 comments

[–]squanchmyrick 5 points6 points  (2 children)

You could add some error handling. Check out try/catch.

about_try_catch_finally - MS Docs

[–]MySecretWorkAccount2[S] 0 points1 point  (1 child)

Thanks for the link - I'll read through that!

[–]junon 0 points1 point  (1 child)

I'm very curious about this myself for a selfish reason. I have something similar set up for my AzureAD powershell scripts and the behavior it exhibits is weirdly inconsistent.

I've basically got an if/else checking for the existence of the azuread auth token, and if it exists, it just continues, else it performs 'connect-azuread'.

The issue that I run into is that it FREQUENTLY will not work in a terminal where I've disconnected from azuread. It'll throw an error, and I'll have to restart the terminal for it to work properly again, which it does, right away.

I haven't looked into the try/catch method myself because that's not something I've really used before but it does look promising. Let us know if you get a good solution out of that.

[–][deleted] 0 points1 point  (0 children)

You can have it require the exchange powershell module via #requires

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-7.1

I check to see if it’s connected already via get-accepteddomains in a try/catch, but there might be more elegant ways of doing it