How to capture warnings in dbatools commands?

656 Views Asked by At

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?

1

There are 1 best solutions below

0
On

A little rough, however, here is what I do:

$response = Invoke-DbaQuery -SqlInstance $sql_host -Database $sql_db -Query $sql_insert_new_response 3>&1
if ($response -match "Failed during execution") {
  Write-Log -Message "Error: $($response.Message). Skipping."
  continue
}