0

Context:

I need to be able to catch warnings that are preventing some backups from being restored when using the Restore-DbaDatabase cmdlet.

I found some posts mentioning that the -Silent parameter for the Restore-DbaDatabase would treat those warnings as errors so I could catch them, but this parameter no longer exists.

I'm using the -EnableException parameter with no luck. Warnings are shown on screen, but I'm unable to catch them in my scripts since no Exception is thrown, as show in the following image:

Restore-DbaDatabase not treating warnings as errors when -EnableException is specified

Question:

What is the right approach to catch/capture all the warnings for a dbatools command?

dsuy
  • 119
  • 5
  • 3
    You need to redirect the **Warning Stream** to stdout. `3>&1`. See [about_Redirection](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-7.1). – Santiago Squarzon May 29 '21 at 19:33
  • 4
    You can also use `$WarningPreference = 'Stop'` at the beginning of your script, then the Warnings will be treated as Exceptions. – Santiago Squarzon May 29 '21 at 19:39
  • You should be able to use the `-WarningVariable` common parameter to capture emitted warnings without stream redirection. I don't know that dbatools supports it. – Bacon Bits May 29 '21 at 22:28

0 Answers0