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.

See more WMI share tasks for PowerShell »

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.

If you like this page then please share it with your friends

 


See more PowerShell share examples including WMI

PowerShell WMI   • Create Win32_Share   • WMI Shares   • Free Permissions Analyzer Tool

Get-Acl  • PowerShell Share Error Codes   • Win32_ComputerSystem  • PowerShell 3.0 CIM

Windows PowerShell  • Free WMI Monitor  • Cacls   • Query   • PowerShell Printer Scripts

Please email me if you have a example scripts.  Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.