strictmode.ps1


<#PSScriptInfo
.VERSION 2.1
.GUID 298fa447-7899-4187-8f64-d30171ccd4ae
.AUTHOR 'Sea Star Development'
.COMPANYNAME 'Sea Star Development'
.COPYRIGHT 'Sea Star Development'
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>


<#
.SYNOPSIS
V2.1 Incorporate Version() and ToString() ScriptMethods, 4 Jan 2013.
A very simple function to retrieve the Set-StrictMode setting of the user
session.
.DESCRIPTION
Retrieve the Set-StrictMode setting for the current session.
This procedure is necessary as there is, apparently, no equivalent PowerShell
variable for this and it enables the setting to be returned to its original
state after possibly being changed within a script. Add this function
to your $profile.
.EXAMPLE
Get-StrictMode
The various values returned will be Version 1, Version 2, or Off.
.EXAMPLE
$a = (Get-StrictMode).Version()
This will allow the environment to be restored just by entering the commmand
Invoke-Expression "Set-StrictMode $a"
#>
 


function Get-StrictMode {
   $errorActionPreference = 'Stop'
   $version = '0'
   try {
      $version = '2'
      $z = "2 * $nil"       #V2 will catch this one.
      $version = '1'
      $z = 2 * $nil         #V1 will catch this.
      $version = 'Off'
   }
   catch {
   }
   $errorActionPreference = 'Continue'
   New-Module -ArgumentList $version -AsCustomObject -ScriptBlock {
      param ([String]$version)
      function Version() {
         if ($version -eq 'Off') {
            [String]$output = '-Off'
          }
         else {
            [String]$output = "-Version $version"
          }
         "$output"                #(Get-StrictMode).Version()
      }
      function ToString() {
         if ($version -ne 'Off') {
            $version = "Version $version"
         }
         "StrictMode: $version"   #Get-StrictMode will output string.
      } 
      Export-ModuleMember -function Version,ToString 
   } 
}