Reusing module connection for all users of a dashboard

Product: PowerShell Universal
Version: 3.7.10

Hi folks, I’m trialing PSU and hoping to use it for data parsing. What’s the best approach to maintaining context of a module, so that all users of my dashboard can run queries without having to connect every time?

For example, an O365 dashboard: Currently I have each user running Connect-MsolService (using tokens / SAM model) every time they run a query.

I would like to have the connection maintained in the background to be re-used across all my dashboard users, if possible. TIA


I ended up creating a separate environment with a persistent runspace, and a startup script that calls the connection. Then assigning the environment to my dashboard.

So this works, but I don’t think it’s the most efficient method, and I’m not clear on how often the runspace is created. The wording in the docs below seems to indicate it happens often.

For dashboards, this will happen whenever a runspace is crated to service an endpoint being run while the user views a dashboard. Busy servers and dashboards with many dynamic components will do this more frequently. For jobs, this will happen once when the job is started.

Runspaces are created when there are none available. With persistence on, it won’t clean up the runspace state and the pool will fill up with connected runspaces. For example, if two users access at the same time, it will have to open 2 runspaces. If two users connect again later, it won’t have to reopen those runspaces.

If one user accesses the dashboard and then another one accesses it later, it will reuse the one runspace.

Thanks. In this use case what would you consider best practice then?

did this actually work for you?

I tried doing this, because I needed to run several Exchange cmdlets in parralel, but it only rarely worked - for some reason, random cmdlets would not detect the active exchange “session” get-connectioninformation could see it just fine as active, but the commandlet wouldn’t work unless I made a new connection - randomly.

I’ve got 5 API’s with exchange cmdlets like the one below, and then run invoke-restmethod in parallel using start-threadjob.

In a persistant environment, it fails 9/10 times, in a normal environment its more like 1/10.

        [Parameter(Mandatory = $true)]
    Import-Module ExchangeOnlineManagement
    if (!(Get-ConnectionInformation | Where-Object {$_.Name -match 'ExchangeOnline' -and $_.state -eq 'Connected'})) { 
        Connect-ExchangeOnline -AppId $Norlys_EXOAppID -CertificateThumbprint $Norlys_CertificateThumbprint -Organization $tenant -ShowBanner:$False | out-null 

    Get-Place -identity $Place | Select *