Constrained Language Mode and Teams Scripts Validation

Hello. I have some Power Shell scripts that I use to manage Teams configuration. Now I want to prepare them for Constrained Language Mode.

I need help because I get some error when I run my scripts.

Look here is my script:

$ExecutionContext.SessionState.LanguageMode

$ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"

$ExecutionContext.SessionState.LanguageMode


$testtext = "Account 1"

if($session -eq $null){


Import-Module -Name MicrosoftTeams

$session = New-CsOnlineSession -Credential $credential -OverrideAdminDomain qg1.onmicrosoft.com
Import-PSSession $session
Enable-CsOnlineSessionForReconnection -erroraction 'silentlycontinue'

Connect-MicrosoftTeams -Credential $credential

Connect-MsolService -Credential $Credential

}


Write-Host "`n`r"


$Switch = Read-Host "`n`rPlease choose from one of the following options: nr
3. I would like to activate Teams user/users with Voice.`n`r
4. I would like to check Teams Voice configuration for user/users.`n`r
5. I would like to check phone license users with voice configuration.`n`r
7. I would like to check users list with phone license and voice parameters.`n`r
9. I would like to create CAP. nr
10.I would like to activate Periscope users.`n`r
11. I would like to make changes in user's voice configuration.`n`r
12. I would like to exit.`n`r
Please enter the number of the option you would like"

switch ($Switch) {
3 {F:\Teams\Teams\Files\GISAppTeamsVoiceActivation4.ps1}
4 {F:\Teams\Teams\Files\GISAppTeamsVoiceActivationChecker4.ps1}
5 {F:\Teams\Teams\Files\GISAppTeamsVoiceGroupChecker.ps1}
7 {F:\Teams\Teams\Files\GISAppO365Check2.ps1}
9 {F:\Teams\Teams\Files\GISAppTeamsCAP.ps1}
10{F:\Teams\Teams\Files\GISAppTeamsVoiceActivationPeriscope3.ps1}
11{F:\Teams\Teams\Files\GISAppTeamsVoiceChange1.ps1}
12{
$s = Get-PSSession
Remove-PSSession -Session $s
exit
}
}

And here is what I get in console:


PS F:\Teams\Teams - Security\Files> F:\Teams\Teams - Security\Files\GISAppTeams.ps1
FullLanguage
ConstrainedLanguage
$PSInvalidCastException/ at System.Management.Automation.LanguagePrimitives.ThrowInvalidConversionException(Object valueToConvert, Type resultType)
at System.Management.Automation.LanguagePrimitives.ConvertNotSupportedConversion(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProv
ider, TypeTable backupTable)
at System.Management.Automation.LanguagePrimitives.ConversionData`1.Invoke(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider,
TypeTable backupTable)
at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, TypeTable backupTypeTable)
at System.Management.Automation.LanguagePrimitives.ConvertTo[T](Object valueToConvert)
at System.Management.Automation.Language.Compiler.NewOutputTypeAttribute(AttributeAst ast)
at System.Management.Automation.Language.Compiler.GetAttribute(AttributeAst attributeAst)
at System.Management.Automation.Language.ScriptBlockAst.<System-Management-Automation-Language-IParameterMetadataProvider-GetScriptBlockAttributes>d_68.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Management.Automation.CompiledScriptBlockData.InitializeMetadata()
at System.Management.Automation.ScriptBlock.get_Attributes()
at Microsoft.Teams.Config.Cmdlets.Runtime.PowerShell.GetScriptCmdlet.<ProcessRecord>b_16_0(FunctionInfo fi)
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Microsoft.Teams.Config.Cmdlets.Runtime.PowerShell.GetScriptCmdlet.ProcessRecord()
Get-ScriptCmdlet : Cannot convert value to type "System.Type[]". Only core types are supported in this language mode.
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\1.1.6\Microsoft.Teams.Config.psm1:42 char:20
+ $cmdletNames = Get-ScriptCmdlet -ScriptFolder $exportsPath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ScriptCmdlet], PSInvalidCastException
+ FullyQualifiedErrorId : ConversionSupportedOnlyToCoreTypes,Microsoft.Teams.Config.Cmdlets.Runtime.PowerShell.GetScriptCmdlet

$PSInvalidCastException/ at System.Management.Automation.LanguagePrimitives.ThrowInvalidConversionException(Object valueToConvert, Type resultType)
at System.Management.Automation.LanguagePrimitives.ConvertNotSupportedConversion(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProv
ider, TypeTable backupTable)
at System.Management.Automation.LanguagePrimitives.ConversionData`1.Invoke(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider,
TypeTable backupTable)
at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, TypeTable backupTypeTable)
at System.Management.Automation.LanguagePrimitives.ConvertTo[T](Object valueToConvert)
at System.Management.Automation.Language.Compiler.NewOutputTypeAttribute(AttributeAst ast)
at System.Management.Automation.Language.Compiler.GetAttribute(AttributeAst attributeAst)
at System.Management.Automation.Language.ScriptBlockAst.<System-Management-Automation-Language-IParameterMetadataProvider-GetScriptBlockAttributes>d_68.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Management.Automation.CompiledScriptBlockData.InitializeMetadata()
at System.Management.Automation.ScriptBlock.get_Attributes()
at Microsoft.Teams.Config.Cmdlets.Runtime.PowerShell.GetScriptCmdlet.<ProcessRecord>b_16_0(FunctionInfo fi)
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Microsoft.Teams.Config.Cmdlets.Runtime.PowerShell.GetScriptCmdlet.ProcessRecord()
Get-ScriptCmdlet : Cannot convert value to type "System.Type[]". Only core types are supported in this language mode.
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\1.1.6\Microsoft.Teams.Config.psm1:43 char:56
+ ... etNames -Alias (Get-ScriptCmdlet -ScriptFolder $exportsPath -AsAlias)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ScriptCmdlet], PSInvalidCastException
+ FullyQualifiedErrorId : ConversionSupportedOnlyToCoreTypes,Microsoft.Teams.Config.Cmdlets.Runtime.PowerShell.GetScriptCmdlet

Cannot invoke method. Method invocation is supported only on core types in this language mode.
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\1.1.6\Microsoft.Teams.Config.psm1:47 char:3
+ $instance.Init();
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage

Thank You for all Your help :slight_smile: