Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources: Active Threads
Viewing all articles
Browse latest Browse all 8411

PowerShell V3 StrictMode question

$
0
0

This may be related to my previous post regarding Set-StrictMode. I have a module that works fine in V2, ie Import-Module Get-StrictMode, etc, but in V3 it just returns 'StrictMode: Off', whatever the StrictMode setting. Code is this:

function Get-StrictMode {
  $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 {
   }
 
   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
   }
}

When run as a normal ps1 script, however, it works fine. Something must have changed and I can't get it to work now. What am I missing please? Sorry abount not using the markup tool but unable to get it to work!


Viewing all articles
Browse latest Browse all 8411

Latest Images

Trending Articles



Latest Images