Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gets the strict mode version for the current scope
function Get-StrictMode {
[CmdletBinding()]
param (
[Switch]$All
)
[Reflection.BindingFlags]$flags = 'Instance, NonPublic'
try {
# SessionStateInternal
$engineSessionState = [System.Management.Automation.SessionState].GetField('sessionState', $flags).
GetValue($executioncontext.SessionState)
# CurrentScope property of SessionStateInternal
$sessionStateScope = $engineSessionState.GetType().GetProperty('CurrentScope', $flags).
GetValue($engineSessionState)
$enumerator = [PowerShell].Assembly.GetType('System.Management.Automation.SessionStateScopeEnumerator').
GetConstructor($flags, $null, $sessionStateScope.GetType(), $null).
Invoke($sessionStateScope)
foreach ($sessionStateScope in $enumerator) {
# StrictModeVersion of SessionStateScope
$version = $sessionStateScope.GetType().GetProperty('StrictModeVersion', $flags).
GetValue($sessionStateScope)
if ($version) {
$version
if (-not $All) {
return
}
}
}
} catch {
throw
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment