Invoke-command in foreach loop : The syntax is not supported by this runspace. This can occur if the runspace is in no-language mode.

Chabango 41 Reputation points
2021-03-15T17:18:01.033+00:00

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
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,455 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Chabango 41 Reputation points
    2021-03-16T13:34:54.47+00:00

    The $search is a variable for each run through the foreach loop. I am not sure how you are saying to parameterize that?

    0 comments No comments

  2. Rich Matheisen 45,831 Reputation points
    2021-03-16T15:04:46.593+00:00

    Three ways to get local values into a scriptblock to be run on a remote machine:

    $search = "A Name Goes Here"
    
    # Make use of the "Using" scope (Probably the best/easiest)
    $name = Invoke-Command -Session (Get-PSSession) -scriptblock {
                Get-CalendarDiagnosticObjects -Identity $Using:search -StartDate 3/10/2021  | 
                    Where-Object {$_.location -eq "WebEx"} | 
                        group-object AppointmentSequenceNumber
        }
    
    # Pass parameter(s) from local machine into the scriptblock
    $name = Invoke-Command -Session (Get-PSSession) -scriptblock {}
                param ([string]$SearchParam)
                Get-CalendarDiagnosticObjects -Identity $SearchParam -StartDate 3/10/2021  | 
                    Where-Object {$_.location -eq "WebEx"} | 
                        group-object AppointmentSequenceNumber
            } -ArgumentList $search
    
    # Build your scriptblock (if the script block is complex this might be the most flexible way)
    # watch out for using double quotes in the scriptblock and the need to use "$($object.property)" to
    # ensure that variable interpolation works the way you want
    $sb = [ScriptBlock]::Create( "Get-CalendarDiagnosticObjects -Identity $search -StartDate 3/10/2021  | 
                                    Where-Object {$_.location -eq 'WebEx'} | 
                                        group-object AppointmentSequenceNumber"
                                )
    $name = Invoke-Command -Session (Get-Session) -ScriptBlock $sb
    
    0 comments No comments

  3. Chabango 41 Reputation points
    2021-03-16T15:41:58.29+00:00

    Thanks for the great info!
    What if the info in the $search variable is not local?
    In my example, $search = $m.primarysmtpaddress, which is pulling from the
    $mbx= Invoke-Command -Session (Get-PSSession) -scriptblock {Get-Mailbox -ResultSize unlimited | select-object primarysmtpaddress}
    in for the foreach loop, $m.primarysmtpaddress would be the primarysmtpaddress for each object in $mbx.
    I'm not sure how to tie in the $search using your example.


  4. Chabango 41 Reputation points
    2021-03-16T19:45:29.493+00:00

    Using your portion of the code, I still receive the same error:
    The syntax is not supported by this runspace. This can occur if the runspace is in no-language mode.

    • CategoryInfo : ParserError: (Get-CalendarDia...tSequenceNumber:String) [], ParseException
    • FullyQualifiedErrorId : ScriptsNotAllowed
    • PSComputerName : outlook.office365.com

    With this code:

    $mbx = Invoke-Command -Session (Get-PSSession) -ScriptBlock {get-mailbox -ResultSize unlimited | select-object primarysmtpaddress} 
    
    $count=1
    $fullcount = ($mbx | measure-object).count
    
    #$mbx = get-exomailbox charles.bost
    
    $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"
    
    ### your portion of the code###
     $sb = [ScriptBlock]::Create( "Get-CalendarDiagnosticObjects -Identity $search -StartDate 3/10/2021  | 
                                     Where-Object {$_.location -eq 'WebEx'} | 
                                         group-object AppointmentSequenceNumber"
                                 )
     $name = Invoke-Command -Session (Get-PSSession) -ScriptBlock $sb
    
     ### end of your portion of code ###
    
    $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