Uploaded image for project: 'Modules'
  1. Modules
  2. MODULES-2634

PowerShell Module doesn't run template with try/catch

    XMLWordPrintable

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • None
    • powershell
    • Windows 2012r2 x64
      PowerShell 4

    • 1
    • Windows 2016-04-20, Windows 2016-05-04, Windows 2016-05-18, Windows 2016-06-01, Windows 2016-06-15
    • Reviewed
    • 1 - 1-5% of Customers
    • 2 - Annoyance
    • 3 - $$$$

    Description

      This is some strange behavior.

      With puppetlabs-powershell v1.0.5, I'm seeing that if I have a templated script, and I wrap a try catch around it, it no longer executes the script.

      Create a module.

      In manifests init.pp, do the following:

      exec { 'test_try_catch':
          command  => template('yourmodule/test.ps1.erb'),
          provider => powershell,
          logoutput=> true,
      }
      

      In templates folder, add a file called test.ps1.erb:

      Write-Host "Should log this message."
       
      try {
        # make it long running
        Write-Host "Sleeping for 10 seconds."
        Start-Sleep 10
       
        # intentionally throw an error:
        $filePath = Join-Path $nonExistent 'scripts'
       
      } catch {
        Write-Host "$($_.Exception.Message)"
        exit 1
      }
      

      Watch it run almost in 1 second.

      Now comment out or remove the try/catch in the erb:

      Write-Host "Should log this message."
       
      #try {
        # make it long running
        Write-Host "Sleeping for 10 seconds."
        Start-Sleep 10
       
        # intentionally throw an error:
        $filePath = Join-Path $nonExistent 'scripts'
       
      #} catch {
      #  Write-Host "$($_.Exception.Message)"
      #  exit 1
      #}
      

      Note that it runs and throws an error now.

      You can also attempt to add $ErrorActionPreference = 'Stop' to the script. Any ideas on this one?

      Attachments

        1. trycatch.png
          253 kB
          Daniel Lu
        2. trycatch2.png
          51 kB
          Daniel Lu

        Issue Links

          Activity

            People

              erick Erick Banks
              rob Rob Reynolds
              Votes:
              1 Vote for this issue
              Watchers:
              12 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Zendesk Support