Constrained Language Mode issue in script

Hello. I have that small part of code:

$ExecutionContext.SessionState.LanguageMode
$ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"
$ExecutionContext.SessionState.LanguageMode

$credential = Get-Credential

$session = New-CsOnlineSession -Credential $credential
Import-PsSession $session

And I get that error:

Import-PsSession : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
At F:\Teams\Teams_Security\Files\GISAppTeams.ps1:64 char:1
+ Import-PsSession $session
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-PSSession], ArgumentOutOfRangeException
    + FullyQualifiedErrorId : System.ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.ImportPSSessionCommand

Does anybody know what issue could that be and how to resolve it?

It looks like you are missing some code here. Where did you assign $session? I don’t see it assigned in what you posted and reading the error message it looks like that is the issue. The error message is telling you it’s a problem with the Index parameter of Import-PSSession which you passed $session as as argument.

It is here :slight_smile: :slight_smile: :slight_smile:

I tried to duplicate it but, New-CsOnlineSession has been deprecated and not available in my environment (MicrosoftTeams module version 2.0). I wonder if this has anything to do with your error. Just a thought but perhaps even with the older version, constrainedlanguage mode may be cause issues with this deprecated command meaning $Session in your case has some issue. You should check the type and properties of that variable after it is assigned to see if it is correct for the session parameter of Import-PSSession. The docs say it should be of type <System.Management.Automation.Runspaces.PSSession>

Here’s the docs on the deprecated command if you are interested. New-CsOnlineSession (SkypeForBusiness) | Microsoft Docs