Skip to content

Instantly share code, notes, and snippets.

@indented-automation
Last active August 2, 2018 11:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indented-automation/9279592035ca952360ce9e33643ba932 to your computer and use it in GitHub Desktop.
Save indented-automation/9279592035ca952360ce9e33643ba932 to your computer and use it in GitHub Desktop.
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