PowerShell WMI

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator



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.

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

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.

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 PowerShell share examples including WMI

PowerShell WMI   • Win32_Share   • WMI Shares   • Win32_ComputerSystem   • Query

Get-Acl   • Set-Acl   • PowerShell Create Share   • PowerShell Error Codes

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.

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

WebThis Site

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

 

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

Please report a broken link, or an error.