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!