# Constrained Language Mode issue in script

**URL:** <https://forums.powershell.org/t/constrained-language-mode-issue-in-script/16109>\
**Category:** PowerShell Help\
**Created:** [March 30, 2021, 6:24am UTC](https://forums.powershell.org/t/constrained-language-mode-issue-in-script/16109 "2021-03-30T06:24:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![msabat](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@msabat](https://forums.powershell.org/u/msabat)\
**Post date:** [March 30, 2021, 6:24am UTC](https://forums.powershell.org/t/constrained-language-mode-issue-in-script/16109/1 "2021-03-30T06:24:17Z")

</div>

Hello. I have that small part of code:

```auto
$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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [March 30, 2021, 12:02pm UTC](https://forums.powershell.org/t/constrained-language-mode-issue-in-script/16109/2 "2021-03-30T12:02:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![msabat](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@msabat](https://forums.powershell.org/u/msabat)\
**Post date:** [March 30, 2021, 5:18pm UTC](https://forums.powershell.org/t/constrained-language-mode-issue-in-script/16109/3 "2021-03-30T17:18:31Z")

</div>

> [@msabat](#):
>
> `$session = New-CsOnlineSession -Credential $credential`

It is here 🙂 🙂 🙂

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [March 30, 2021, 5:43pm UTC](https://forums.powershell.org/t/constrained-language-mode-issue-in-script/16109/4 "2021-03-30T17:43:09Z")

</div>

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](https://docs.microsoft.com/en-us/powershell/module/skype/new-csonlinesession?view=skype-ps)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/constrained-language-mode-issue-in-script/16109/5 "2024-05-16T20:27:00Z")

</div>

Closed on May 16, 2024, 8:27pm UTC


