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()