Powershell - Add try catch block.

Can some one help me in adding try and catch block to the below code to effectively handle exceptions and write meaningful messages when exception occurs.
$o365module = get-module -name tmp_*
$msolmodule = get-module -name msonline


if( $O365module -eq $null -or $msolmodule -eq $null){
		
	if($O365session -eq $null -or $O365session.State -ne "Opened"){

		$user = "myown@mydomain.onmicrosoft.com"
		$password = Get-content	 "C:\temp\password.txt" | convertto-securestring
	
		$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$password
	
		$O365Session= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credentials -Authentication Basic -AllowRedirection -warningaction silentlycontinue

	}

		$temp = Import-PSSession $O365session -allowclobber -WarningAction SilentlyContinue -DisableNameChecking

		$moduleName = $temp.Name

		Import-Module msonline
		Connect-MsolService -Credential $credentials -warningaction silentlycontinue

}
else
{
write-host "$o365Session already exists"
}

Open in new window

LVL 1
Learner_newBieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

n2fcCommented:
Do you need to catch specific errors? all errors?
Are you concerned over the "get-module" lines or more?

Various examples are available, but the following link should guide you directly through the process and has many good examples:

http://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell

As a start, I would put your 1st 2 lines in a try{ } block adding "-ErrorAction Stop" at the end of the "get-module" lines...
Then follow with a "catch" block as shown in the tutorial...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learner_newBieAuthor Commented:
Hi N2fc,

I am looking to catch all the errors. Yes I am bit concerned about the get-module
n2fcCommented:
Have you tried my suggestion yet? How has that worked for you?
Learner_newBieAuthor Commented:
Hi N2FC,

I will try and post you if I get desired result.

Thanks.
Learner_newBieAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.