Properly testing data model regeneration batch file for errors when called from PowerShell

Hey folks, I’m working on an automatically refreshing dev environment, and ended up doing it all in a PowerShell script after messing around with SSIS for a week. My prototype works well but when I tried to build in a few reporting / debugging steps, I lost the plot.

I should mention I started from a place of not knowing PowerShell very much at all (it’s just CMD except you can use ls instead of dir, right?), and it has been a fascinating journey.

My batch file actually works, and logs success to a .txt log. However, I wanted to be able to test each step for success. After a lot of digging online I found out about %ERRORLEVEL% and then $LASTEXITCODE and started with each step being coded something like this:

if($successTest){
    & $pathFolder\myBatchFile.bat
     if($LASTEXITCODE  -gt 0){
          $logStatus += "this thing failed"  + "`n"
          $successTest = 0
          throw
     } else {
          $logStatus += "this thing done" + "`n"
     }
} 

For every step (delete task agent, create task agent, recycle App Pool) the batch file runs and returns 0 in $LASTEXITCODE. This is good. At the end I email myself $logStatus regardless of the result in a Finally clause.

Except for this step, where the generate data model actually runs and succeeds, but returns a 1.

I realize if I knew beans about PowerShell I’d probably know this, but I have searched everywhere I can think of and haven’t come up with anything.

Any thoughts? Basically either along the lines of “no you idiot that’s not how you test a batch file for errors” or “no you idiot getting a 1 is terrible and will bring your system down”, or anything in between!

Is there any documentation for running the generation model .exe I am going to start doing these myself. Sorry that I am not able to help since I am also fairly new to powershell as well, for now i am doing small scripts and its being quite interesting . All I’ve done is , get the app pools, copy files over to epicor server remotely . What I am going to do next is recycle the app pool (seems easy), then either check if the are some active users connected to epicor and finally try to regenerate the data model and recycle. If you need help let me know and I can send you what I’ve done so far.