If you use the excellent tools from dbatools.ie to manage your SQL Server, you may worry about what happens if it all goes wround.  Maybe the resatore files were corrupted wne copied from another server or not in the directory you specified.  Well, I worry about stuff, so I tried to find a solution.  This is a little complicated at first glance but essentially all it does is

  1. Run Restore-DbaDatabase in the background as a ‘Job’ and wait for it to finish
  2. Check output of the Job for ant failures
  3. Handle any failures

Run the job from SQL Agent, as an ‘Operating system (CmdExec)’ type.  In the Job Step Properties you will notice the successful exit code is set to ‘0’ (zero).  The code above caused the script to exit with exit code 1, should an issue be encountered.  The will cause the SQL Agent job to report as a FAIL.

You can play around with the captured errors and add more details about the cause of the error.

One Response to Handling errors with Restore-DbaDatabase

Leave a Reply

Your email address will not be published. Required fields are marked *