1
Vote

Invoke-SqlRestore with -FileListOnly doesn't return the actual exact error in case there is one

description

Hi,

First of all, I am a Powershell and SQLPSX rookie that really appreciates your work and if I am being wrong with my logic here or there is another quick way to bypass the below issue please be understanding :)

Ok, so I am working on a script that is using the Invoke-SqlRestore function of the SQLServer module for restoring an SQL Server DB.
But BEFORE the actual restore I need to check the DB files structure and for this I am using the same Invoke-SqlRestore function, this time with -FileListOnly parameter "true".

Now the issue I am facing and the quick improvement idea I think I found:

Issue: If for some reason the Invoke-SqlRestore with -FileListOnly fails then the inner error (that contains the actual reason for the fail) is not returned in the calling script, only a generic one (my scenarios so far: user without restore or create DB permissions, DB backup on different database level than the target server).

I looked over the source code of the Invoke-SqlRestore function and I noticed that on that mechanism for handling the -FileListOnly "path" there is only a $restore.ReadFileList($server) statement followed by a return.
In order to return the actual error on this "path" also, same as for the actual restore case I did a quick test and added the same try/catch mechanism here (in that if ($FileListOnly) statement) and it worked.
Actual mechanism:
if ($FileListOnly)
{ 
    $restore.ReadFileList($server)
    return
}
Updated mechanism:
if ($FileListOnly)
{ 

              try { $restore.ReadFileList($server) }
                catch {
                        $ex = $_.Exception
                        $message = $ex.message
                        $ex = $ex.InnerException
                        while ($ex.InnerException)
                        {
                            $message += "`n$ex.InnerException.message"
                            $ex = $ex.InnerException
                        }
                        Write-Error $message
                }
    return
}

So again, this worked for my test, I was able to "see" the hidden errors that made my script to fail in those mentioned scenarios.

My question here: Would it make sense to add the try/catch with the actual error capturing mechanism in the "FileListOnly mechanism" also?

Many thanks and regards,
Eduard

comments