Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Getting more error messages


In this recipe, we will learn how to display additional error messages.

How to do it...

Let's take a look at how to display additional error messages:

  1. Open PowerShell ISE as an administrator.

  2. Add the following script and run it:

    Clear-Host
    $error[0] | Format-List -Force

How it works...

PowerShell supports some variables called automatic variables that store the state information or environment information, such as arguments, events, user directories, profile information, or error codes. The $error is an automatic array variable that holds all the error objects that are encountered in your PowerShell session. To display the last error message, you can use the following code:

$error[0] | 
Format-List -Force

This method is particularly useful when you are working with SQL Server and you encounter an exception. When you run this command, you get the full stack trace; therefore, you get a more complete picture of exactly what went wrong. For example, when you run your script...