Powershell session function or module.

Hi All,

We run powershell scripts through web interface to manage operational activity. Each time we run the below powershell script it loads a new session, downloads all the cmdlets which is taking time. Even for creating a new mailbox we had to wait for the session to be loaded each time.

Out application has provisioned to share runspace and maintain powershell session consitently without creating session.

Could you please help me in creating a powershell session module psm1 file so that we will it while our users access the website for the first time. In the script we will call this module only if session -eq NULL.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

o365AdmAuthor Commented:
Below is the cmdlet which we use to get powershell session. We want this in a powershell module all together so that it will be easy for us to call.

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

Import-Module msonline
Connect-MsolService -Credential $Credentials -warningaction silentlycontinue
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You are using a PS Host (https://technet.microsoft.com/en-us/library/ee706563(v=vs.85).aspx)? In that, you should have to import the remote session once only, and have full access without redoing any import action.
o365AdmAuthor Commented:
Hi Qlemo,

The requested page cannot be found. May I request to resend the link or article on again.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Sorry, the parens and question mark are not part of the link, but parsed by EE that way:

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
o365AdmAuthor Commented:
Thank you Qlemo.
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

From novice to tech pro — start learning today.