Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 240697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:41:18+00:00 2026-05-11T20:41:18+00:00

First off, either A) I’m not investigating into this hard enough or B) I’ve

  • 0

First off, either A) I’m not investigating into this hard enough or B) I’ve found a problem that requires some funky hack. By the way this is posh v1.0.

Here it goes:

A week or so ago I asked a question about redirecting the output from the exection of an EXE in powershell that was otherwise not being caught. I was swiftly presented with “2>&1” which solved the problem.

Now I’ve hit another snag and hope to see what some of you stackoverflowers can throw at it.

I’m using try-catch blocks throughout my code as a good programmer should. When I went to place a call to GPG (gnupg.org), passing it a few commands as follows:

try `
{
    & $gpgExeLocation --import $keyFileName 2>&1 | out-file "theOutput.txt";
} `
-Catch `
{
    write-host "$_";
}

I get a blank text file (theOutput.txt).

But if I do the same call outside of the try-catch block, the text file gets some text written to it as expected.

What I’m wondering is if there is an issue with output redirection to stdout and the way powershell traps exceptions – or if it is my try-catch code to begin with?

here is my try-catch implementation

function global:try
{
    param
    (
        [ScriptBlock]$Command = $(Throw "The parameter -Command is required."),
        [ScriptBlock]$Catch   = { Throw $_ },
        [ScriptBlock]$Finally = {}
    )

    & {
        $local:ErrorActionPreference = "SilentlyContinue"

        trap
        {
            trap
            {
                & {
                    trap { Throw $_ }
                    &$Finally
                }

                Throw $_
            }

            $_ | & { &$Catch }
        }

        &$Command
    }

    & {
        trap { Throw $_ }
        &$Finally
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:41:18+00:00Added an answer on May 11, 2026 at 8:41 pm

    It appears you are using a custom Try function with a -Catch parameter. Mind sharing your implementation to see if that could be causing the problem?

    BTW I doubt that your catch statement would ever be invoked unless you are converting the non-terminating error condition of $lastexitode -ne 0 to a terminating error. In this case, you may be better off with a function like this. I use it a lot (it’s quite handy):

    function Get-CallStack {
        trap { continue }
        1..100 | foreach {
            $var = Get-Variable -scope $_ MyInvocation
            $var.Value.PositionMessage -replace "`n"
        }
    }
    
    #--------------------------------------------------------------------
    # Helper function to deal with legacy exe exit codes
    #--------------------------------------------------------------------
    function CheckLastExitCode {
        param ([int[]]$SuccessCodes = @(0), [scriptblock]$CleanupScript=$null)
    
        if ($SuccessCodes -notcontains $LastExitCode) {
            if ($CleanupScript) {
                "Executing cleanup script: $CleanupScript"
                &$CleanupScript
            }
            $OFS = $NL = [System.Environment]::NewLine
            throw "EXE RETURNED EXIT CODE ${LastExitCode}${NL}$(Get-CallStack)"
        }
    }
    

    Use it like so:

    & $gpgExeLocation --import $keyFileName 2>&1 | out-file "theOutput.txt"
    CheckLastExitCode
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I'm working on an app that's written such that some of your
First off, let me start off that I am not a .net developer. The
First off: I know that this isn't reliable for actually checking if I can
First off, I am using Windows XP. I have multiple hard drives and it
First off, there's a bit of background to this issue available on my blog:
First off, this question is ripped out from this question. I did it because
First off, I apologize if this doesn't make sense. I'm new to XHTML, CSS
First off, I'll admit that I'm anal about such things. (Bad, bad, me.) However,
First off, I am not a DBA, but I do work in an environment
First off I am using the Codeigniter Framework so this issue is a workaround

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.