locked
How to suppress exit code 0? RRS feed

  • Question

  • I have two powershell scripts.

    script1 is a script that is supposed to be a wrapper and passes parameters to script2. script2 generates data based on those passed parameters.

    script1 has currently a for loop the iterates a command like this:

    foreach()
    {   
        cmd /c "openPS.bat script2.ps1 $($someParam[-1])"
    }

    everytime that command runs successfully, it outputs exit 0

    exit0

    as you can see, in one of the runs, there was an error in script2 in which i specified to output exit 1 + $error[0] whenever there is an error/exception. but nowhere in script2 did i specify exit 0. there is only for exit 1 (error).

    i read suggestions to use | out-null after the command, but the problem is the error from script2 no longer gets outputted/reported.

    can i say something like cmd /c "openPS.bat script2.ps1 $($someParam[-1]) | out-null only exit 0?

    Thursday, February 21, 2019 4:03 AM

All replies

  • can i say something like cmd
    /c "openPS.bat script2.ps1 $($someParam[-1]) | out-null only exit 0
    ?

    Yes. You can say that.  I can't say it but that is because I don't understand that language.

    My first question is why would you want to say that?

    What does "\c … " mean?

    You can start here:

    help out-null -online

    If you need help understanding that then go here: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276?l=r54IrOWy_2304984382

    Those two links can help you with this.

    This link will also be helpful: https://westmesatech.com/?p=56


    \_(ツ)_/

    Thursday, February 21, 2019 5:01 AM
  • can i say something like cmd
    /c "openPS.bat script2.ps1 $($someParam[-1]) | out-null only exit 0
    ?

    Yes. You can say that.  I can't say it but that is because I don't understand that language.

    My first question is why would you want to say that?

    What does "\c … " mean?

    You can start here:

    help out-null -online

    If you need help understanding that then go here: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276?l=r54IrOWy_2304984382

    Those two links can help you with this.

    This link will also be helpful: https://westmesatech.com/?p=56


    \_(ツ)_/

    I gotta admit, I laughed at that part ”yes, you can say that. I cant say that”

    Love the sarcasm 😂😂😂

    Anyways, /c is part of cmd /c. It's a call to a command in powershell to invoke other scripts from this script. 

    And all I want is a way to make an exception of out-null to exclude exit code 1, or to just nullify all exit code 0's, that are apparent in the image 

    Thursday, February 21, 2019 5:10 AM
  • And all I want is a way to make an exception of out-null to exclude exit code 1, or to just nullify all exit code 0's, that are apparent in the image 

    Then why didn't you just ask that. "Ou-Null" has no exceptions. Read the help.

    Also, what do you mean by "make an exception"?


    \_(ツ)_/

    Thursday, February 21, 2019 5:22 AM
  • And all I want is a way to make an exception of out-null to exclude exit code 1, or to just nullify all exit code 0's, that are apparent in the image 

    Then why didn't you just ask that. "Ou-Null" has no exceptions. Read the help.

    Also, what do you mean by "make an exception"?


    \_(ツ)_/

    Well, if i put ou-null, exit0 goes away which is wat i want. However, of there are errors (exit 1), it also gets suppressed and doesn't get output. I want to make this an exceptional case, so out-null ignores exit1 errors

    Thursday, February 21, 2019 5:35 AM
  • Out-Null cannot ignore anything.  Please read the help link I posted.


    \_(ツ)_/

    Thursday, February 21, 2019 5:37 AM
  • You can filter any output like this:

    …. | Where-Object{ $_ -match 'exit 1'}


    \_(ツ)_/

    • Proposed as answer by jrv Tuesday, February 26, 2019 10:52 AM
    Thursday, February 21, 2019 5:41 AM
  • You can filter any output like this:

    …. | Where-Object{ $_ -match 'exit 1'}


    \_(ツ)_/

    Out-null | where-object?
    Thursday, February 21, 2019 5:57 AM
  • $errorcode=cmd/c "openPS.bat script2.ps1 $($someParam[-1])"
    If ($_ -match 'exit 1')
    {Write-Host "Failed"}
    else{Out-Null}


    Just do it.

    Monday, February 25, 2019 7:11 AM
  • Just to show the complete "picture"

    https://powershell.org/forums/topic/how-to-suppress-exit-0/


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Monday, February 25, 2019 7:16 AM
  • $errorcode=cmd/c "openPS.bat script2.ps1 $($someParam[-1])"
    If ($_ -match 'exit 1')
    {Write-Host "Failed"}
    else{Out-Null}


    Just do it.

    Unexpected token '$_' in expression or statement.
    Monday, February 25, 2019 4:22 PM
  • Unexpected token '$_' in expression or statement.
    You really should start to learn the basics of Powershell .... including how to debug your own scripts. As we cannot see your screen and you do not show your complete code including the batch file you're using we have barely enough information to be able to help you.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Monday, February 25, 2019 5:36 PM

  • If ($_ -match 'exit 1')

    should be

    If ($errorcode -match 'exit 1')

    in this case.

    Tuesday, February 26, 2019 10:30 AM
  • This must be run in a pipeline:  Se my post above.


    \_(ツ)_/

    Tuesday, February 26, 2019 10:53 AM
  • change $_ to $errorcode.

    Also, please follow BOFH's suggestion.


    Just do it.


    Wednesday, February 27, 2019 9:29 AM