Windows PowerShell


Display Error Codes with PowerShell

Display Error Codes with Microsoft PowerShell

My mission on this page is to show you how to display error codes, furthermore, I will explain how to translate meaningless numbers into meaningful phrases.

Topics for Error Codes in PowerShell

 ♣

Mission to Display Error Messages When Creating Shares

We need a vehicle to examine error codes.  That vehicle will be creating a file share.  For example, if the share already exists, when you run the script again, it returns error code 22.  Not only can we trap this return value, but also we can 'Switch' 22 to a more meaningful message, such as 'Duplicate Share'

Example 1: How to Create a PowerShell Function

Naturally, any function needs a name, in this case I chose 'errMsg'.  As this is a simple function, it only has one argument, $intErr. The rest of the function is taken up with a 'Switch' statement, or a series of statements corresponding to each error value.

# Microsoft PowerShell Function only
# Author: Guy Thomas
# Version 1.1 March 2008 tested on PowerShell v 1.0

Function errMsg($intErr)
{
Switch($intErr)
{
0 { "Success - Share created" }
2 { "Access denied - Permission?" }
8 { "Unknown failure" }
9 { "Invalid name" }
10 { "Invalid level" }
21 { "Invalid parameter" }
22 { "Duplicate share - Already created" }
23 { "Redirected path" }
24 { "Unknown device or directory" }
25 { "Net name not found" }
DEFAULT { "$intErr has an Unknown value" }
}
}

Note 1:  This code does not do anything by itself, it is merely a stage in producing the script.

Example 2: Using a PowerShell Function to Display an Error Code

Preparation and Pre-requisites

To understand Example 2, I suggest that you familiarise yourself with the logic of my 'Create a Share' example.  Then you can match my thinking to the second portion of the script below.

When you understand my script, copy and paste the code into PowerShell, or better, create a cmdlet with .ps1 extension.  Then call that file from within PowerShell with ./filename.

# Using a PowerShell Function
# Author: Guy Thomas
# Version 2.3 March 2008 tested on PowerShell v 1.0

Function errMsg($intErr)
{
   Switch($intErr)
     {
      0 { "Success - Share created" }
      2 { "Access denied - Permission?" }
      8 { "Unknown failure" }
      9 { "Invalid name" }
    10 { "Invalid level" }
    21 { "Invalid parameter" }
    22 { "Duplicate share - Already created" }
    23 { "Redirected path" }
    24 { "Unknown device or directory" }
    25 { "Net name not found" }
     DEFAULT { "$intErr has an Unknown value" }
     }
}

$FolderPath = "C:\Temp"
$ShareName = "Temporary"
$Type = 0
$Description = "Guy and PowerShell"
$class = "Win32_share"
$objWMI = [wmiClass] 'Win32_share'
$Success=$objWMI.create($FolderPath, $ShareName, $Type)
errMsg($Success.returnValue)

Note 1:  Working backwards! On the last line, errMsg($Success.returnValue) runs a number through the function, which I created in the first part of the script.  The result is a meaningful message controlled by that function's Switch statement.

Note 2:  Next, let us study the $Success variable.  $Success=$objWMI.... results in an attempt to create the share, however, it exits, therefore the .returnValue is 22.

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Summary: Error Codes in PowerShell

All good Microsoft PowerShell scripts should have error correcting code.  The first step is to trap the .returnValue; closely followed by switching the number for a meaningful phrase.

See more Microsoft PowerShell tutorials:

PowerShell Home  • Com  • Shell Application  • Active Directory  • QAD Snap-in  • Get-Member

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.