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

PowerShell Module doesn't run template with try/catch

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: powershell
    • Labels:
    • Environment:

      Windows 2012r2 x64
      PowerShell 4

    • Template:
    • Story Points:
      1
    • Sprint:
      Windows 2016-04-20, Windows 2016-05-04, Windows 2016-05-18, Windows 2016-06-01, Windows 2016-06-15
    • CS Priority:
      Reviewed
    • CS Frequency:
      1 - 1-5% of Customers
    • CS Severity:
      2 - Annoyance
    • CS Business Value:
      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
          trycatch.png
          253 kB
        2. trycatch2.png
          trycatch2.png
          51 kB

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved:

                  Zendesk Support