Last active
August 2, 2018 11:58
-
-
Save indented-automation/9279592035ca952360ce9e33643ba932 to your computer and use it in GitHub Desktop.
Gets the strict mode version for the current scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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