Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request]: Non-Zero Exit Code For Console-Host #52

Closed
AdamDanischewski opened this issue Nov 16, 2024 · 5 comments
Closed

[Request]: Non-Zero Exit Code For Console-Host #52

AdamDanischewski opened this issue Nov 16, 2024 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@AdamDanischewski
Copy link

Describe the feature or improvement you're requesting

When testing a script that uses winget-install to install winget if not present, I noticed that the captured process has an exit code of 1 not 0 although successful. When looking at the code, it's totally intentional because I'm running it in consolehost not sure why the logic is like this (its my first foray into a PS script) but it looks like Powershell sets the exit code to 1 when you exit via a break statement.

I'd like your script to return 0 regardless of if I'm launching from consolehost or not.

Here is the winget-install code:

function ExitWithDelay {
...
    # Exit the script with error code
    if ($host.Name -eq 'ConsoleHost') {
        Break
    } else {
        Exit $ExitCode
    }
}

Here is the part where I use it from my code, that I would like to not look for 0,1 yet only 0:

            $wgicmd = Get-Command winget-install | Select-Object -ExpandProperty Definition
            $wgi_cmd_process = Start-Process powershell -Verb RunAs "-Command", "$wgicmd -force -wait" -wait -PassThru
            if ($wgi_cmd_process.ExitCode -in @(0,1)) { 
                Write-host ">> Success, winget is now installed!!" -ForegroundColor Green
            } else { 
                throw "Something went wrong installing winget, exit code: $($wgi_cmd_process.ExitCode)"               
            } 

Additional context

No response

@AdamDanischewski AdamDanischewski added the enhancement New feature or request label Nov 16, 2024
@asheroto
Copy link
Owner

Hey there!

With this last release I updated the code to break instead of exit. The reason for that was that PowerShell kept closing the entire window at the end. So for those running the script locally, breaking would keep the window open, and for scripts running in the background, it would exit with an exit code.

So the challenge is... do we let the script close the PowerShell window but with the right code, or do we let the window remain by breaking but exiting with the wrong exit code.

Ideally I should probably break up the script into additional functions and then run them in order so that the script naturally exits.

As a workaround, I wonder if forcing an error code would work:

# Set the exit code after the break
$global:LASTEXITCODE = 0

I'll have to test when I get back to my computer.

@AdamDanischewski
Copy link
Author

AdamDanischewski commented Nov 17, 2024

Yea I thought that's probably why you wanted to avoid exiting on ConsoleHost, Bash has the same issues.

You may want to look at the $MyInvocation cmdlet -

if ($MyInvocation.CommandOrigin -eq "Runspace") { 
 break
} ... 

[In testing if it's run in a subprocess CommandOrigin will be "Internal"]

@asheroto
Copy link
Owner

asheroto commented Nov 18, 2024

Thanks! That worked great. Updating now. I'm not sure how it will appear in other languages, any idea?

@uffemcev can you try this? Do you get Runspace when running this command:

$MyInvocation.CommandOrigin -eq "Runspace"

@uffemcev
Copy link
Contributor

uffemcev commented Nov 22, 2024

@asheroto Hello, sorry for late answer. It's that what you need?

image

{1CF9AD49-3892-41D6-B586-76B564EEEEC4}

@asheroto
Copy link
Owner

Perfect, thank you! Just wanted to make sure it worked with non-English systems. Looks like they use Runspace as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants