Powershell.com’s Tip Of The Day–Avoid Use Of Set-StrictMode In Production? No Way!

I appreciate the Powershell tips from Powershell.com.

But the Monday, 3/21/11 tip recommends avoiding the use of Set-StrictMode “latest” in production, and I think that’s bad advice.  Here’s a quote from the tip:


However, Set-StrictMode should only be used on development machines. You should never use it on production machines or in production scripts as PowerShell will not complain about non-existing properties. Have a look:

Dir $env:windir | Where-Object { $_.Length -gt 1MB }


I think the line of code in the example contains a bug, and it’s a bug we should catch during testing–but if we miss it we should catch it in production rather than simply assuming our script ran correctly.  Therefore, rather than avoid using Set-StrictMode we should fix the bug. 

Here’s a little test script that sets up some test directories, uses a line like the one above (which causes an error), then demonstrates a better-coded version that accomplishes what we want with no errors, even with Set-StrictMode “latest” in effect.

$ErrorActionPreference = "stop"
Set-StrictMode -Version "latest"

$testroot = $env:temp + "\PSTipsTest"

# set up a test directory with subdirectories and files, for use later on.
try
{
    if (Test-Path $testroot)
    {
        Remove-Item $testroot -Recurse
    }

    New-Item -ItemType directory -Path $testroot -Force | Out-Null

    New-Item -ItemType file -Path "$testroot\testfile.txt" -Value "test file" -force | Out-Null

    $subdirA = "$testroot\subdir1"
    New-Item -ItemType directory -Path $subdirA -Force | Out-Null
    New-Item -ItemType file -Path "$subdirA\anothertestfile.txt" -Value "test file" -force | Out-Null

    $subdirB = "$testroot\subsub2"
    New-Item -ItemType directory -Path $subdirB -Force | Out-Null
    New-Item -ItemType file -Path "$subdirB\yetanothertestfile.txt" -Value "test file" -force | Out-Null
}
catch
{
    throw "Error setting up test: $_"
}

# show the directory structure we just created
cls
#dir $testroot -Recurse
#"`r`n`r`n"

# this is the equivalent of the code from the Powershell tip.
# Its recommendation was to not use Set-StrictMode -Version "latest" because you’d get an error.
# I disagree–it’s buggy code and catching it with Set-StrictMode is a good thing.
try
{
    Dir $testroot  |
        Where-Object { $_.Length -lt 1MB } |
        select name

}
catch
{
    "The dir command failed due to a bug.  $_"    # you’ll see this error occur.
}

"`r`n`r`n"

# I think this code is better.  You can use Set-StrictMode, and its intent is also clearer.
try
{
    Dir $testroot  |
        Where-Object {!$_.PSIsContainer} |
        Where-Object { $_.Length -lt 1MB } |
        select name
}
catch
{
    "this error should not happen: $_"    # you won’t see this error occur.
}

Besides the obvious desire to catch bugs rather than mask them, there’s another reason to use Set-StrictMode:  if we’re using it in our test environment but not in production, that means that our production processes don’t match our test ones.  We should never run scripts in production that have not been run in test.  Code of any kind—Powershell scripts or otherwise—should not be changed on its way into production.  Administrative code is no different in that regard than any other code.

In summary, use Set-StrictMode “latest” in production, if at all possible, because:

  • It catches nasty bugs
  • It keeps scripts in test environments and production environment in step.
Advertisements

2 Responses to “Powershell.com’s Tip Of The Day–Avoid Use Of Set-StrictMode In Production? No Way!”

  1. Norman Skinner Says:

    You could just code defensively and check for the property existence first, like this:

    Dir $env:windir | Where-Object { (Get-Member –Name Length –InputObject $_) -and ($_.Length -gt 1MB)}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: