# Help with exec() function

**URL:** <https://forums.powershell.org/t/help-with-exec-function/14951>\
**Category:** PowerShell Help\
**Created:** [September 15, 2020, 6:30am UTC](https://forums.powershell.org/t/help-with-exec-function/14951 "2020-09-15T06:30:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![powerslearner135](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@powerslearner135](https://forums.powershell.org/u/powerslearner135)\
**Post date:** [September 15, 2020, 6:30am UTC](https://forums.powershell.org/t/help-with-exec-function/14951/1 "2020-09-15T06:30:04Z")

</div>

Hi,  
I am using this exec() function ( [PowerShell Exec function from psake · GitHub](https://gist.github.com/jpoehls/2843721) ) in my script where I have couple commands which I am using with this function.  
When I am using the function, and create an error in one of the commands - the script continues to execute - it does not fail on the error.  
I created another script where I am not using the function and I created an error ( delete some file etc. so I can produce an error):  
ex.

**& gulp clean**   
**if (! $?) { throw “Gulp clean failed” }**  
 _& gulp build_   
In this case, the script fails and it does not continue on the other tasks - ( task gulp build is skipped)  
On the other hand,with exec() function,  
ex.

**function Exec**

**{**

 **param(**

 **[Parameter(Mandatory=1)][scriptblock]$cmd,**

 **[Parameter(Mandatory=1)][string]$task**

 **)**

 **& $cmd**

 **if (! $?) {**

 **throw ("$task" + 'failed!')****} ****Exec { gulp clean } "Gulp clean"** _Exec { gulp build} "Gulp build"_

In this case, it gives the error on the screen but continues to run the script with the other tasks - for example continues with gulp build.

How can I resolve this issue and make it stop executing on the first error just like it does when not using exec() function?  
Thank you

&nbsp;

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [September 15, 2020, 7:09am UTC](https://forums.powershell.org/t/help-with-exec-function/14951/2 "2020-09-15T07:09:49Z")

</div>

If you look at the Free Resources link on the left, there is a ebook on Error handling. You can try a standard try\catch:

```
Try {
  $ErrorActionPreference="stop"
  & gulp clean
}
Catch {
  Write-Warning "Failed to find $username. $($_.Exception.Message)"
}
```

---

<div class="post-metadata">

**Author:** ![powerslearner135](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@powerslearner135](https://forums.powershell.org/u/powerslearner135)\
**Post date:** [September 15, 2020, 7:27am UTC](https://forums.powershell.org/t/help-with-exec-function/14951/3 "2020-09-15T07:27:33Z")

</div>

Thanks for the recommendation.  
I already tried with try/catch, but again it gives me the error and continues to execute.

---

<div class="post-metadata">

**Author:** ![adminofthings](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adminofthings](https://forums.powershell.org/u/adminofthings)\
**Post date:** [September 16, 2020, 3:52am UTC](https://forums.powershell.org/t/help-with-exec-function/14951/4 "2020-09-16T03:52:23Z")

</div>

You should use automatic variable `$LastExitCode` for this rather than `$?`:

&nbsp;

```
function Exec
{
    param(
        [Parameter(Mandatory=1)][scriptblock]$cmd,
        [Parameter(Mandatory=1)][string]$task
    )
    & $cmd
    if ($LastExitCode -ne 0) {
       throw (“$task” + ‘failed!’)
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/help-with-exec-function/14951/5 "2024-05-16T20:28:51Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


