Rename of logfile name just prior to exit of script

Currently, I have my logfile names set to be named as “$envComputerName $deploymentType $exitCode” and the toolkit does this, but I would like to have it rename the logfile just prior to exit so it shows the final exit code…or really just anything that’s not a defer, quick retry, success. All of the logs just show 0 at the end of the name, even after the install.

So I have a couple lines I added in the final Catch section of Deploy-Application.ps1 that rename the logfile to “$envComputerName $deploymentType $lastExitCode”. When I set some test variables (i.e. fake exit code) and run this on its own outside of the script it does rename the logfile as requested, but does not work inside the script. I believe it has something to do with the variable being cleared and maybe the placement of the lines would help this, but not quite sure. Any ideas or assistance is greatly appreciated.

You shouldn’t be doing that in the Catch block. You only go into the Catch block upon an error. You need to add a Finally block and do the log file rename in that as the Finally block is the last thing that will run before script exit.

Ahh that’s right, I forgot about Finally {}. Thank you very much! Now I just tried that, but at the very end of the Catch block is Exit-Script -ExitCode 1 so, if there is an error, doesn’t that mean the script will exit before it even reaches the Rename-Item in the Finally block?

Ok disregard. It’s working great in the Finally block. Thanks again, much appreciated!!