Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Windows Server Automation with PowerShell Cookbook - Fourth Edition

You're reading from  Windows Server Automation with PowerShell Cookbook - Fourth Edition

Product type Book
Published in Jul 2021
Publisher Packt
ISBN-13 9781800568457
Pages 674 pages
Edition 4th Edition
Languages
Concepts
Author (1):
Thomas Lee Thomas Lee
Profile icon Thomas Lee
Toc

Table of Contents (18) Chapters close

Preface 1. Installing and Configuring PowerShell 7 2. Introducing PowerShell 7 3. Exploring Compatibility with Windows PowerShell 4. Using PowerShell 7 in the Enterprise 5. Exploring .NET 6. Managing Active Directory 7. Managing Networking in the Enterprise 8. Implementing Enterprise Security 9. Managing Storage 10. Managing Shared Data 11. Managing Printing 12. Managing Hyper-V 13. Managing Azure 14. Troubleshooting with PowerShell 15. Managing with Windows Management Instrumentation 16. Other Books You May Enjoy
17. Index

Exploring the error view and Get-Error

Since the very beginning, Windows PowerShell has done a great job in displaying the results of errors: a big blob of red text on a black background that contains full details about what went wrong. It was tremendous, but many new users found it a bit off-putting – there was too much information, some of which was not very useful in most cases.

PowerShell 7 now offers a more concise view of errors that reduces the amount of text and improves the format of the output. The result is shorter and more readable output. And, on those rare occasions when it might be necessary, you can Get-Error to get complete error details without having to parse through $Error[0].

Getting ready

You run this recipe on SRV1 after you have installed PowerShell 7 and/or Visual Studio Code, and once you have created a console profile file.

How to do it...

  1. Creating a simple script
    $SCRIPT = @'
      # divide by zero
      42/0  
    '@
    $SCRIPTFILENAME = 'C:\Foo\ZeroDivError.ps1'
    $SCRIPT | Out-File -Path $SCRIPTFILENAME
    
  2. Running the script and seeing the default error view
    & $SCRIPTFILENAME
    
  3. Running the same line from the console
    42/0
    
  4. Viewing the $ErrorView variable
    $ErrorView
    
  5. Viewing the potential values of $ErrorView
    $Type = $ErrorView.GetType().FullName
    [System.Enum]::GetNames($Type)
    
  6. Setting $ErrorView to 'NormalView' and recreating the error
    $ErrorView = 'NormalView'
    & $SCRIPTFILENAME
    
  7. Setting $ErrorView to 'CategoryView' and recreating the error
    $ErrorView = 'CategoryView'
    & $SCRIPTFILENAME
    
  8. Setting $ErrorView to its default value
    $ErrorView = 'ConciseView'
    

How it works...

In step 1, you create a script that contains a (deliberate) divide-by-zero error. This step creates the file, but creates no other output.

In step 2, you run the script from within VS Code, and view the resulting error, which looks like this:

Figure 2.43: Running the script and viewing the error

In step 3, you create a divide-by-zero error from the command line. The output from this step looks like this:

Figure 2.44: Running the same line from the console

PowerShell 7 uses the built-in $ErrorView variable to hold the name of the error view PowerShell should use to display errors. In step 4, you view the current value of this variable, which looks like this:

Figure 2.45: Viewing the value of the $ErrorView variable

The $ErrorView variable can take one of three values, as you can see from the output of step 5:

Figure 2.46: Viewing the potential values of $ErrorView

In step 6, you set the value of $ErrorView to display the error using the output generated by Windows PowerShell and then re-view the error, which looks like this:

Figure 2.47: Setting $ErrorView to NormalView and recreating the error

In step 7, you set $ErrorView to display the error using CategoryView and then recreate the error. The output from this step shows the category error view:

Figure 2.48: Setting $ErrorView to CategoryView and recreating the error

In step 8, you reset the value of $ErrorView to the default value. This step creates no output.

There's more...

The concise error view you see in the output from step 2 contains all the information from the standard view that you can see in the output from step 7, except for the omission of the error category information. And if you invoke the error directly from the command line, as shown in step 3, you see only the error message, which is easier on the eyes.

In step 5, you view the error category information. In most cases, this is not particularly useful.

In step 8, you reset the value of $ErrorView. Depending on what you are doing, this step may not be needed. You can just exit the PowerShell console (or VS Code), and the next time you start PowerShell, it resets the value back to the default (ConciseView). And if you should prefer the normal or category error views, you can always set a value to $ErrorView in your profile file.

Although not shown in this recipe, you can use the Get-Error cmdlet to show you complete error information about a specific error. For most IT Professionals, the basic error information provided by PowerShell 7 is more than adequate (and a great improvement over error output with Windows PowerShell).

You have been reading a chapter from
Windows Server Automation with PowerShell Cookbook - Fourth Edition
Published in: Jul 2021 Publisher: Packt ISBN-13: 9781800568457
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}