Persistent Environment How To

Product: PowerShell Universal
Version: 3.7.7

Hi all, hoping for a little guidance. I’ve got a pretty simple use case, and I just cant quite figure it out. Basically I want to use a persistent runspace that maintains an active connection to exchange online.
That way I can can use that runspace with other scripts and api’s to run exchange commands without having the load up time of running Connect-ExchangeOnline each time i want to make a quick mailbox command.

  1. If anyone has a better solution, I’m totally open to suggestions

  2. Here’s my use case so far.

I created a pretty basic environment, marked it as persistent, added a couple modules, and created a start up script (Script below) hoping that I could connect to exchange when the environment loads.

StartupScript.ps1

try {
    Connect-ExchangeOnline -CertificateThumbprint $thumbprint -AppId $appID  -Organization $org -ShowBanner:$false -ErrorAction Stop
    "Connected"
} catch {
    "Error Connecting"
    $Error[0]
}

My hope is that I could write a script or endpoint that could just reference that environment with exchange connected

So I made a quick script, set the environment as my persistent environment, ran it and errored out from not recognizing he exchange commands – which makes me think that my persistent environment didn’t import the modules and connect to exchange. Where am I going wrong?

Exchange cmdlets will fail with “is not regognized as the name of a cmdlet” if it doesnt detect an active exchange session.

I get this error all the time in persistent runspace even though Get-ConnectionInformation shows an active session.

Can the exchange session be stored in a variable, and then used with invoke-command -session $session?