Powershell: Getting a useful error message

So I’ve been writing a lot of Powershell scripts lately. Particularly with respect to SharePoint config and SQL Backup & Restores.

Running some of these restores has been problematic and I’d quote often see this unhelpful error.

Exception calling "SqlRestore" with "1" argument(s): "Restore failed for Server 'demo2010a'. "
At C:\Scripts\RestoreDB.ps1:104 char:23
+ $smoRestore.SqlRestore <<<< ($server)

So what really went wrong here?
Enter this command:

$error[0]|format-list –force

Basically the errors that have occurred are put into a stack and this command allows you to reference then array style where the 0th element is the most recent error and format-list –force outputs a full stack trace of the exception that occurred.

In my case I didn’t have exclusive access to the database which is required for a restore, so I was able to add this line to me restore script:

$server.KillAllprocesses($dbname)

Hope you find this useful

-Gavin

Advertisement
This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to Powershell: Getting a useful error message

  1. Jim says:

    Thanks!
    That’s awesome info and helped me. I had the same issue and was able to confirm by looking at the error stack. Thanks again!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.