I am running an invoke-command against Exchange Online in a foreach loop and IO keep receiving the following error: The syntax is not supported by this runspace. This can occur if the runspace is in no-language mode. Here is the code: the first invoke-command ($MBX) outside the foreach completes fine, but the second ($name) throws the error:
$mbx= Invoke-Command -Session (Get-PSSession) -scriptblock {Get-Mailbox -ResultSize unlimited | select-object primarysmtpaddress}
$count=1
$fullcount = ($mbx | measure-object).count
$Meetings = @()
foreach ($M in $MBX) {
$count++
$search = $m.primarysmtpaddress
Write-progress -activity "Getting Calendar $count out of $fullcount --- $search " -percentcomplete (($count / $fullcount)*100) -status "Processing"
#### THis is where the error happens ######
$name = Invoke-Command -Session (Get-PSSession) -scriptblock {Get-CalendarDiagnosticObjects -Identity $search -StartDate 3/10/2021 | where {$_.location -eq "WebEx"} | group-object AppointmentSequenceNumber}
$User = New-Object psobject
$user | Add-Member -MemberType NoteProperty -Name Mailbox -Value $null
$user | Add-Member -MemberType NoteProperty -Name WebExCount -Value $null
$user.mailbox = $M.primarysmtpaddress
$user.WebExCount = $name.count
$meetings += $user
}
$meetings