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.
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'
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
Note 1: This code does not do anything by
itself, it is merely a stage in producing the script.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, Solarwinds
have created the
WMI Monitor so that you can examine these gems of
performance information for free. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.
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
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.
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.
Please write in if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
WMI Monitor so that you can examine these gems of
performance information for free. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.