Exchange Online Session and Runspace

I need to execute Get-MailboxStatistics in a Runspace. I can connect to Exchange online. If I do a ‘Get-Pssession’ I can see the Exchange session. But how do I pass this ExchangeOnline session to the Runspace to execute Get-MailboxStatistics. Currently it does not recognize the Get-MailboxStatistics command in Runspace.

Here is my code (this is part of a larger script):

# Connecting to Exchange Online
$AdminName = "hil119"
$Pass = "password"
$cred_cloud = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
Connect-ExchangeOnline -Credential $cred_cloud -Prefix Cloud

# Executing Get-MailboxStatistics in a thread
$Runspace = [runspacefactory]::CreateRunspace()
$PowerShell = [powershell]::Create()
$PowerShell.runspace = $Runspace
$Runspace.Open()
[void]$PowerShell.AddScript({Get-MailboxStatistics 'd94589'})
$PowerShell.BeginInvoke()

 

AFAIK, it won’t. You will have toc create the session inside the runspace.

So you are saying that for each thread I need to create a new Exchange Online Session? That would defeat the purpose of having threads.

I need to specify a single instance of Exchange Online Session and apply that to all threads.