Powershell equivalent of Perl's $CHILD_ERROR Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!Is there an equivalent of 'which' on the Windows command line?Setting Windows PowerShell path variableDetermine installed PowerShell versionHow to run a PowerShell scriptPowerShell says “execution of scripts is disabled on this system.”Unix tail equivalent command in Windows PowershellHow to pass boolean values to a PowerShell script from a command promptPowerShell exit code - Calling from MSBuildHow do you comment out code in PowerShell?Multiple arguments for powershell command

Can inflation occur in a positive-sum game currency system such as the Stack Exchange reputation system?

Antler Helmet: Can it work?

When is phishing education going too far?

How do I keep my slimes from escaping their pens?

The logistics of corpse disposal

How much radiation do nuclear physics experiments expose researchers to nowadays?

How discoverable are IPv6 addresses and AAAA names by potential attackers?

What is this single-engine low-wing propeller plane?

Should I call the interviewer directly, if HR aren't responding?

Check which numbers satisfy the condition [A*B*C = A! + B! + C!]

If 'B is more likely given A', then 'A is more likely given B'

What do you call a plan that's an alternative plan in case your initial plan fails?

Models of set theory where not every set can be linearly ordered

Proof involving the spectral radius and the Jordan canonical form

How can I fade player character when he goes inside or outside of the area?

When to stop saving and start investing?

Why was the term "discrete" used in discrete logarithm?

How widely used is the term Treppenwitz? Is it something that most Germans know?

How to find all the available tools in macOS terminal?

Gastric acid as a weapon

What are the motives behind Cersei's orders given to Bronn?

How can I make names more distinctive without making them longer?

Is there a concise way to say "all of the X, one of each"?

iPhone Wallpaper?



Powershell equivalent of Perl's $CHILD_ERROR



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!Is there an equivalent of 'which' on the Windows command line?Setting Windows PowerShell path variableDetermine installed PowerShell versionHow to run a PowerShell scriptPowerShell says “execution of scripts is disabled on this system.”Unix tail equivalent command in Windows PowershellHow to pass boolean values to a PowerShell script from a command promptPowerShell exit code - Calling from MSBuildHow do you comment out code in PowerShell?Multiple arguments for powershell command



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I essentially require a functionality in Powershell that executes the given string (it can be a CMD/Powershell command, a perl/python/powershell with arguments or an exe with arguments, etc) captures its exit value.
In perl I would pass the string to 'system()' and use the '$CHILD_ERROR' perlval and shift it to access the exit code.



In powershell I am clueless.



I tried using Invoke-Expression, but even if the expression passed to Invoke-Expression fails, the Invoke-Expression call itself will have succeeded.










share|improve this question



















  • 1





    Pro-tip: it is excessively rare to find a valid use-case for Invoke-Expression. Avoid it whenever possible. What you want is the call operator (&)

    – TheIncorrigible1
    Mar 8 at 17:14











  • Using & sets the $? correctly, so I will know whether the command succeeded or failed. BUT, it won't set the $LASTEXITCODE to the command's return value.

    – MRUDUL DOSHIT
    Mar 9 at 3:34

















-1















I essentially require a functionality in Powershell that executes the given string (it can be a CMD/Powershell command, a perl/python/powershell with arguments or an exe with arguments, etc) captures its exit value.
In perl I would pass the string to 'system()' and use the '$CHILD_ERROR' perlval and shift it to access the exit code.



In powershell I am clueless.



I tried using Invoke-Expression, but even if the expression passed to Invoke-Expression fails, the Invoke-Expression call itself will have succeeded.










share|improve this question



















  • 1





    Pro-tip: it is excessively rare to find a valid use-case for Invoke-Expression. Avoid it whenever possible. What you want is the call operator (&)

    – TheIncorrigible1
    Mar 8 at 17:14











  • Using & sets the $? correctly, so I will know whether the command succeeded or failed. BUT, it won't set the $LASTEXITCODE to the command's return value.

    – MRUDUL DOSHIT
    Mar 9 at 3:34













-1












-1








-1


0






I essentially require a functionality in Powershell that executes the given string (it can be a CMD/Powershell command, a perl/python/powershell with arguments or an exe with arguments, etc) captures its exit value.
In perl I would pass the string to 'system()' and use the '$CHILD_ERROR' perlval and shift it to access the exit code.



In powershell I am clueless.



I tried using Invoke-Expression, but even if the expression passed to Invoke-Expression fails, the Invoke-Expression call itself will have succeeded.










share|improve this question
















I essentially require a functionality in Powershell that executes the given string (it can be a CMD/Powershell command, a perl/python/powershell with arguments or an exe with arguments, etc) captures its exit value.
In perl I would pass the string to 'system()' and use the '$CHILD_ERROR' perlval and shift it to access the exit code.



In powershell I am clueless.



I tried using Invoke-Expression, but even if the expression passed to Invoke-Expression fails, the Invoke-Expression call itself will have succeeded.







windows powershell perl exit exit-code






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 16:14









choroba

160k14142211




160k14142211










asked Mar 8 at 16:03









MRUDUL DOSHITMRUDUL DOSHIT

283




283







  • 1





    Pro-tip: it is excessively rare to find a valid use-case for Invoke-Expression. Avoid it whenever possible. What you want is the call operator (&)

    – TheIncorrigible1
    Mar 8 at 17:14











  • Using & sets the $? correctly, so I will know whether the command succeeded or failed. BUT, it won't set the $LASTEXITCODE to the command's return value.

    – MRUDUL DOSHIT
    Mar 9 at 3:34












  • 1





    Pro-tip: it is excessively rare to find a valid use-case for Invoke-Expression. Avoid it whenever possible. What you want is the call operator (&)

    – TheIncorrigible1
    Mar 8 at 17:14











  • Using & sets the $? correctly, so I will know whether the command succeeded or failed. BUT, it won't set the $LASTEXITCODE to the command's return value.

    – MRUDUL DOSHIT
    Mar 9 at 3:34







1




1





Pro-tip: it is excessively rare to find a valid use-case for Invoke-Expression. Avoid it whenever possible. What you want is the call operator (&)

– TheIncorrigible1
Mar 8 at 17:14





Pro-tip: it is excessively rare to find a valid use-case for Invoke-Expression. Avoid it whenever possible. What you want is the call operator (&)

– TheIncorrigible1
Mar 8 at 17:14













Using & sets the $? correctly, so I will know whether the command succeeded or failed. BUT, it won't set the $LASTEXITCODE to the command's return value.

– MRUDUL DOSHIT
Mar 9 at 3:34





Using & sets the $? correctly, so I will know whether the command succeeded or failed. BUT, it won't set the $LASTEXITCODE to the command's return value.

– MRUDUL DOSHIT
Mar 9 at 3:34












1 Answer
1






active

oldest

votes


















2














You can use $LASTEXITCODE to get the exit code from an external program or the Boolean $? to check if the last operation succeeded or failed. Run Get-Help about_Automatic_Variables -ShowWindow from a PowerShell console to see more details.



You may want to check out the & (call) command as an alternative to Invoke-Expression when running external programs. Run Get-Help about_Automatic_Variables -ShowWindow from a PowerShell console for details.



Also remember you may be able to just call the external program without using one of the commands above. See the example below:



param($Hostname="127.0.0.1", $Tries=1, $Wait=1000)
$output = ping.exe $Hostname -n $Tries -w $Wait # captures anything written to stdout
$output|? $_ -match 'Request timed out'|Write-Warning
$LASTEXITCODE # returns the exit code from ping.exe


You can copy it to a test.ps1 file and run it from a PowerShell console window (.test.ps1 8.8.8.8 for instance) to see how it works.






share|improve this answer

























  • In my scenario I will always get the command as a parameter string, so I will need someone to execute it. Lets say the command is: $command = "mkdir aaa:bbbccc", which is sent as a script parameter. This command will obviously fail! But, after the statement: $output = Invoke-Expression $command, the $LASTEXITCODE is 0 (probably because the invoke-expression successfully fired mkdir, even though mkdir failed) and $? is also $True.

    – MRUDUL DOSHIT
    Mar 9 at 3:37











  • In this scenario you are running PowerShell's built-in mkdir command so $LASTEXITCODE is not set but you could use the call (&) command and trap the exception to handle the error. On the other hand, if you need to run the mkdir command from cmd.exe you'll need code like this: $cmd="cmd.exe"; $params="/c mkdir dir1dir2"; &$cmd $params; $LASTEXITCODE. Run it twice, it should succeed (and print 0) the first time and fail (and print 1) the second one as the folder will already exist.This way $LASTEXITCODE is populated as we are running an external program.

    – camilohe
    Mar 21 at 18:51











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55066850%2fpowershell-equivalent-of-perls-child-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use $LASTEXITCODE to get the exit code from an external program or the Boolean $? to check if the last operation succeeded or failed. Run Get-Help about_Automatic_Variables -ShowWindow from a PowerShell console to see more details.



You may want to check out the & (call) command as an alternative to Invoke-Expression when running external programs. Run Get-Help about_Automatic_Variables -ShowWindow from a PowerShell console for details.



Also remember you may be able to just call the external program without using one of the commands above. See the example below:



param($Hostname="127.0.0.1", $Tries=1, $Wait=1000)
$output = ping.exe $Hostname -n $Tries -w $Wait # captures anything written to stdout
$output|? $_ -match 'Request timed out'|Write-Warning
$LASTEXITCODE # returns the exit code from ping.exe


You can copy it to a test.ps1 file and run it from a PowerShell console window (.test.ps1 8.8.8.8 for instance) to see how it works.






share|improve this answer

























  • In my scenario I will always get the command as a parameter string, so I will need someone to execute it. Lets say the command is: $command = "mkdir aaa:bbbccc", which is sent as a script parameter. This command will obviously fail! But, after the statement: $output = Invoke-Expression $command, the $LASTEXITCODE is 0 (probably because the invoke-expression successfully fired mkdir, even though mkdir failed) and $? is also $True.

    – MRUDUL DOSHIT
    Mar 9 at 3:37











  • In this scenario you are running PowerShell's built-in mkdir command so $LASTEXITCODE is not set but you could use the call (&) command and trap the exception to handle the error. On the other hand, if you need to run the mkdir command from cmd.exe you'll need code like this: $cmd="cmd.exe"; $params="/c mkdir dir1dir2"; &$cmd $params; $LASTEXITCODE. Run it twice, it should succeed (and print 0) the first time and fail (and print 1) the second one as the folder will already exist.This way $LASTEXITCODE is populated as we are running an external program.

    – camilohe
    Mar 21 at 18:51















2














You can use $LASTEXITCODE to get the exit code from an external program or the Boolean $? to check if the last operation succeeded or failed. Run Get-Help about_Automatic_Variables -ShowWindow from a PowerShell console to see more details.



You may want to check out the & (call) command as an alternative to Invoke-Expression when running external programs. Run Get-Help about_Automatic_Variables -ShowWindow from a PowerShell console for details.



Also remember you may be able to just call the external program without using one of the commands above. See the example below:



param($Hostname="127.0.0.1", $Tries=1, $Wait=1000)
$output = ping.exe $Hostname -n $Tries -w $Wait # captures anything written to stdout
$output|? $_ -match 'Request timed out'|Write-Warning
$LASTEXITCODE # returns the exit code from ping.exe


You can copy it to a test.ps1 file and run it from a PowerShell console window (.test.ps1 8.8.8.8 for instance) to see how it works.






share|improve this answer

























  • In my scenario I will always get the command as a parameter string, so I will need someone to execute it. Lets say the command is: $command = "mkdir aaa:bbbccc", which is sent as a script parameter. This command will obviously fail! But, after the statement: $output = Invoke-Expression $command, the $LASTEXITCODE is 0 (probably because the invoke-expression successfully fired mkdir, even though mkdir failed) and $? is also $True.

    – MRUDUL DOSHIT
    Mar 9 at 3:37











  • In this scenario you are running PowerShell's built-in mkdir command so $LASTEXITCODE is not set but you could use the call (&) command and trap the exception to handle the error. On the other hand, if you need to run the mkdir command from cmd.exe you'll need code like this: $cmd="cmd.exe"; $params="/c mkdir dir1dir2"; &$cmd $params; $LASTEXITCODE. Run it twice, it should succeed (and print 0) the first time and fail (and print 1) the second one as the folder will already exist.This way $LASTEXITCODE is populated as we are running an external program.

    – camilohe
    Mar 21 at 18:51













2












2








2







You can use $LASTEXITCODE to get the exit code from an external program or the Boolean $? to check if the last operation succeeded or failed. Run Get-Help about_Automatic_Variables -ShowWindow from a PowerShell console to see more details.



You may want to check out the & (call) command as an alternative to Invoke-Expression when running external programs. Run Get-Help about_Automatic_Variables -ShowWindow from a PowerShell console for details.



Also remember you may be able to just call the external program without using one of the commands above. See the example below:



param($Hostname="127.0.0.1", $Tries=1, $Wait=1000)
$output = ping.exe $Hostname -n $Tries -w $Wait # captures anything written to stdout
$output|? $_ -match 'Request timed out'|Write-Warning
$LASTEXITCODE # returns the exit code from ping.exe


You can copy it to a test.ps1 file and run it from a PowerShell console window (.test.ps1 8.8.8.8 for instance) to see how it works.






share|improve this answer















You can use $LASTEXITCODE to get the exit code from an external program or the Boolean $? to check if the last operation succeeded or failed. Run Get-Help about_Automatic_Variables -ShowWindow from a PowerShell console to see more details.



You may want to check out the & (call) command as an alternative to Invoke-Expression when running external programs. Run Get-Help about_Automatic_Variables -ShowWindow from a PowerShell console for details.



Also remember you may be able to just call the external program without using one of the commands above. See the example below:



param($Hostname="127.0.0.1", $Tries=1, $Wait=1000)
$output = ping.exe $Hostname -n $Tries -w $Wait # captures anything written to stdout
$output|? $_ -match 'Request timed out'|Write-Warning
$LASTEXITCODE # returns the exit code from ping.exe


You can copy it to a test.ps1 file and run it from a PowerShell console window (.test.ps1 8.8.8.8 for instance) to see how it works.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 17:08

























answered Mar 8 at 16:35









camilohecamilohe

441512




441512












  • In my scenario I will always get the command as a parameter string, so I will need someone to execute it. Lets say the command is: $command = "mkdir aaa:bbbccc", which is sent as a script parameter. This command will obviously fail! But, after the statement: $output = Invoke-Expression $command, the $LASTEXITCODE is 0 (probably because the invoke-expression successfully fired mkdir, even though mkdir failed) and $? is also $True.

    – MRUDUL DOSHIT
    Mar 9 at 3:37











  • In this scenario you are running PowerShell's built-in mkdir command so $LASTEXITCODE is not set but you could use the call (&) command and trap the exception to handle the error. On the other hand, if you need to run the mkdir command from cmd.exe you'll need code like this: $cmd="cmd.exe"; $params="/c mkdir dir1dir2"; &$cmd $params; $LASTEXITCODE. Run it twice, it should succeed (and print 0) the first time and fail (and print 1) the second one as the folder will already exist.This way $LASTEXITCODE is populated as we are running an external program.

    – camilohe
    Mar 21 at 18:51

















  • In my scenario I will always get the command as a parameter string, so I will need someone to execute it. Lets say the command is: $command = "mkdir aaa:bbbccc", which is sent as a script parameter. This command will obviously fail! But, after the statement: $output = Invoke-Expression $command, the $LASTEXITCODE is 0 (probably because the invoke-expression successfully fired mkdir, even though mkdir failed) and $? is also $True.

    – MRUDUL DOSHIT
    Mar 9 at 3:37











  • In this scenario you are running PowerShell's built-in mkdir command so $LASTEXITCODE is not set but you could use the call (&) command and trap the exception to handle the error. On the other hand, if you need to run the mkdir command from cmd.exe you'll need code like this: $cmd="cmd.exe"; $params="/c mkdir dir1dir2"; &$cmd $params; $LASTEXITCODE. Run it twice, it should succeed (and print 0) the first time and fail (and print 1) the second one as the folder will already exist.This way $LASTEXITCODE is populated as we are running an external program.

    – camilohe
    Mar 21 at 18:51
















In my scenario I will always get the command as a parameter string, so I will need someone to execute it. Lets say the command is: $command = "mkdir aaa:bbbccc", which is sent as a script parameter. This command will obviously fail! But, after the statement: $output = Invoke-Expression $command, the $LASTEXITCODE is 0 (probably because the invoke-expression successfully fired mkdir, even though mkdir failed) and $? is also $True.

– MRUDUL DOSHIT
Mar 9 at 3:37





In my scenario I will always get the command as a parameter string, so I will need someone to execute it. Lets say the command is: $command = "mkdir aaa:bbbccc", which is sent as a script parameter. This command will obviously fail! But, after the statement: $output = Invoke-Expression $command, the $LASTEXITCODE is 0 (probably because the invoke-expression successfully fired mkdir, even though mkdir failed) and $? is also $True.

– MRUDUL DOSHIT
Mar 9 at 3:37













In this scenario you are running PowerShell's built-in mkdir command so $LASTEXITCODE is not set but you could use the call (&) command and trap the exception to handle the error. On the other hand, if you need to run the mkdir command from cmd.exe you'll need code like this: $cmd="cmd.exe"; $params="/c mkdir dir1dir2"; &$cmd $params; $LASTEXITCODE. Run it twice, it should succeed (and print 0) the first time and fail (and print 1) the second one as the folder will already exist.This way $LASTEXITCODE is populated as we are running an external program.

– camilohe
Mar 21 at 18:51





In this scenario you are running PowerShell's built-in mkdir command so $LASTEXITCODE is not set but you could use the call (&) command and trap the exception to handle the error. On the other hand, if you need to run the mkdir command from cmd.exe you'll need code like this: $cmd="cmd.exe"; $params="/c mkdir dir1dir2"; &$cmd $params; $LASTEXITCODE. Run it twice, it should succeed (and print 0) the first time and fail (and print 1) the second one as the folder will already exist.This way $LASTEXITCODE is populated as we are running an external program.

– camilohe
Mar 21 at 18:51



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55066850%2fpowershell-equivalent-of-perls-child-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко