In RestrictedLanguage mode, an instance of a given type cannot be created with a cast, but can be created with New-Object
See original GitHub issueNote: I may have a misconception here, but the difference in behavior strikes me as unusual.
Steps to reproduce
try {
$ps = [powershell]::Create()
$ps.Runspace.SessionStateProxy.LanguageMode = 'Restricted'
try {
$ps.AddScript('[regex] "."').Invoke()
$ps.Streams.Error | Write-Warning
}
catch { Write-Warning $_ }
$ps.Commands.Clear(); $ps.Streams.ClearStreams()
try {
$ps.AddScript('New-Object regex "."').Invoke()
$ps.Streams.Error | Write-Warning
}
catch { Write-Warning $_ }
}
finally { $ps.Dispose() }
Expected behavior
Both attempts to create a [regex] instance should fail, because it appears that [regex] is not on an apparent, but undocumented whitelist of permitted types (the whitelist is only documented for ConstrainedLanguage); by contrast, a type such as [int] can be constructed successfully with either method ([int] 0 and New-Object int).
Actual behavior
The cast method fails, whereas the New-Object method succeeds:
WARNING: Exception calling "Invoke" with "0" argument(s): "At line:1 char:1
+ [regex] "."
+ ~~~~~~
+ The type regex is not allowed in restricted language mode or a Data section."
Options RightToLeft MatchTimeout
------- ----------- ------------
None False -00:00:00.0010000
Environment data
PowerShell Core 7.1.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:23 (15 by maintainers)
Top Results From Across the Web
about Language Modes - PowerShell
In RestrictedLanguage mode, users can run commands (cmdlets, functions, CIM commands, and workflows), but can't use script blocks.
Read more >PowerShell New-Object blocked on some Constrained ...
Hi all, I have a PowerShell script that is being distributed by Intune to a number of PCs. The script basically creates a...
Read more >Automatically retrieve Allowed Types for Constrained ...
Is there a way to do this? Or: How can I check (in full language mode) if a specific type is compliant with...
Read more >A Deep Dive into PowerShell's Constrained Language Mode
Cmdlet parameters that convert string input to types work only when the resulting type is an allowed type. The ToString() method and the...
Read more >"Only Core Types are supported in this language mode" error
The ConstrainedLanguage mode permits all cmdlets and all PowerShell language elements, but it limits permitted types.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@TravisEz13
Aren’t language modes all about security? If not, what purpose do they serve? Are only some modes security-relevant? If so, which? The linked
about_Language_Modesconceptual help topic doesn’t provide any clues.So what is the resolution here?
New-Objectto instantiate it either?New-Objectallows you to in instantiate a type, casts should allow it too?RestrictedLanguagemode, given that the error messageThe type regex is not allowed in restricted language mode or a Data section.implies that there is one?I set Issue-Bug and WG-Security until team triages this.