Update : I found some sort of a "solution", but i'm not sure it's completely OK as now I don't use this code anymore, i use the "EXO" cmdlets from the module "Exchange Online Powershell v2", and it seems reliable.
The solution I think of is to add some "fake" items to the runspacecollection to deal with the "disconnect", here is the updated pseudo-code (i could not identify my updates, the main part is the addition of a parameter "disconnect" to the scriptBlock).
$curScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
$ScriptBlock = {
Param($num,$disconnect)
if ($disconnect) {
Disconnect-ExchangeOnline -Confirm:$false
}
else {
num=$num"
$p=Get-mailbox XXXXXXXX -erroraction stop |select primarysmtpaddress
"primarysmtpaddress=$($p.primarysmtpaddress)"
$totalitemsize=Get-o365mailboxstatistics XXXXXXXX -erroraction stop |select totalitemsize
"totalitemsize=$($totalitemsize.totalitemsize)"
}
}
$RunspaceCollection = @()
Connect O365 using module Exchange Online v2 (i had to include all this in another file ".ps1", i did not figured out how to add it "dynamically")
$initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault2()
$initialSessionState.Variables.Add((New-Object -TypeName System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList "CommandName", @("Get-Mailbox", "Get-MailboxStatistics"), $null) )
$initialSessionState.StartupScripts.Add("$curScriptPath\Connect-O365.ps1")
Create runspace pool
$maxThreads = 3
$runspacepool = [runspacefactory]::CreateRunspacePool(1, $maxThreads, $initialSessionState, $Host)
$RunspacePool.Open()
0..10 | % {
$Powershell = [PowerShell]::Create().AddScript($ScriptBlock).AddArgument($_)
Specify runspace to use
$Powershell.RunspacePool = $RunspacePool
Create Runspace collection
[Collections.Arraylist]$RunspaceCollection += New-Object -TypeName PSObject -Property @{
Runspace = $PowerShell.BeginInvoke()
PowerShell = $PowerShell
}
}
add some "fake" items to the runspacecollection to deal with the "disconnect", 1 per thread
$disconnect=$true
1..$maxThreads | % {
$Powershell = [PowerShell]::Create().AddScript($ScriptBlock).AddArgument($_).AddArgument($disconnect)
$Powershell.RunspacePool = $RunspacePool #Specify runspace pool to use
Add runspace in Runspace collection
[Collections.Arraylist]$RunspaceCollection += New-Object -TypeName PSObject -Property @{
Runspace = $PowerShell.BeginInvoke()
PowerShell = $PowerShell
}
}
Get return of each call
While($RunspaceCollection){
Foreach($Runspace in $RunspaceCollection.ToArray()){
If($Runspace.Runspace.IsCompleted){
$Runspace.PowerShell.EndInvoke($Runspace.Runspace)
$Runspace.PowerShell.Dispose()
$RunspaceCollection.Remove($Runspace)
}
}
}
$RunspacePool.Close()
$RunspacePool.Dispose()