page hit counter
Welcome to MSDN Blogs Sign in | Join | Help

Taylor Brown's Blog

Test Lead for Windows Core OS Division on the Hyper-V Team.

Syndication

News

Welcome to the professional blog of Taylor Brown.  I am a test lead on the core virtualization team (Hyper-V) at Microsoft.

This blog will contain information about virtualization, Microsoft, Hyper-V, operating systems, testing, and whatever else I end up talking about...

 

Standard Microsoft Disclaimer:
"This posting is provided "AS IS" with no warranties, and confers no rights. You assume all risk for your use."

-Taylor Brown


Hyper-V WMI: Rich Error Messages for Non-Zero ReturnValue (no more 32773, 32768, 32700…)

Since I did my last post on snapshots I have gotten several comments and few e-mails asking how to map ReturnValues to actual human readable strings.  I guess not every one just knows that 32773 is Invalid Parameter…  Well after many hours of asking and playing I have the answer – or at least a good start so here it is:

The real magic is this in ProcessWMIJob… it now operates on the pipeline and can take two new parameters one for the WmiClassPath and a second for the MethodName…  It uses those to retrieve the error strings for the specific class and function from WMI…  To use this just put it at the top of your scripts or in your environment and party on…

filter ProcessWMIJob

   
param
    ( 
        [
string]$WmiClassPath = $null,
        [
string]$MethodName = $null
    )
    
   
$errorCode = 0

   
if ($_.ReturnValue -eq 4096)
    { 
       
$Job = [WMI]$_.Job

       
while ($Job.JobState -eq 4)
        { 
           
Write-Progress $Job.Caption "% Complete" -PercentComplete $Job.PercentComplete
           
Start-Sleep -seconds 1
           
$Job.PSBase.Get()
        } 
       
if ($Job.JobState -ne 7)
        { 
           
if ($Job.ErrorDescription -ne "")
            {
               
Write-Error $Job.ErrorDescription
               
Throw $Job.ErrorDescription
            }
           
else
            {
               
$errorCode = $Job.ErrorCode
            }
        } 
       
Write-Progress $Job.Caption "Completed" -Completed $TRUE
    }
   
elseif($_.ReturnValue -ne 0)
    {
       
$errorCode = $_.ReturnValue
    }
    
   
if ($errorCode -ne 0)
    { 
       
Write-Error "Hyper-V WMI Job Failed!"
       
if ($WmiClassPath -and $MethodName)
        {
           
$psWmiClass = [WmiClass]$WmiClassPath
           
$psWmiClass.PSBase.Options.UseAmendedQualifiers = $TRUE
           
$MethodQualifiers = $psWmiClass.PSBase.Methods[$MethodName].Qualifiers
           
$indexOfError = [System.Array]::IndexOf($MethodQualifiers["ValueMap"].Value, [string]$errorCode)
           
if ($indexOfError -ne "-1")
            {
               
Throw "ReturnCode: ", $errorCode, " ErrorMessage: '", $MethodQualifiers["Values"].Value[$indexOfError], "' - when calling $MethodName"
            }
           
else
            {
               
Throw "ReturnCode: ", $errorCode, " ErrorMessage: 'MessageNotFound' - when calling $MethodName"
            }
        }
       
else
        {
           
Throw "ReturnCode: ", $errorCode, "When calling $MethodName - for rich error messages provide classpath and method name."
        }
    } 
   
return $_
}

Example:

$VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization"
$VHDService.CreateDynamicVirtualHardDisk("C:\Users\Public\Documents\Hyper-V\Virtual hard disks\MyVHD.vhd", 20GB) | ProcessWMIJob $VHDService "CreateDynamicVirtualHardDisk"

Revised 7/6 To Add Support For WMI Jobs that have blank Error Descriptions.
Revised 7/28 To Return Created Object on Success and To Add Sample Code...

Taylor Brown
Hyper-V Integration Test Lead
http://blogs.msdn.com/taylorb

clip_image001

Published Wednesday, June 18, 2008 9:29 PM by taylorb

Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Pregnant Man » Hyper-V WMI: Rich Error Messages for Non-Zero ReturnValue (no more 32773, 32768, 32700???) @ Thursday, June 19, 2008 1:09 AM

PingBack from http://wordnew.acne-reveiw.info/?p=243

Pregnant Man » Hyper-V WMI: Rich Error Messages for Non-Zero ReturnValue (no more 32773, 32768, 32700???)

# Hyper-V WMI: KVP Exchange aka Data Exchange (Adding New Items From Parent/Host) @ Sunday, July 06, 2008 6:35 PM

I have gotten a ton of questions about the KVP Exchange or Data Exchange Integration Component… 

Taylor Brown's Blog

# Using The Virtual Disk Service (VDS) From Powershell to Mount and Use VHD's @ Friday, September 19, 2008 2:16 PM

This is by far the longest post both in length and the time it took to figure it out I have ever done

Taylor Brown's Blog

# Virtual Disk Service (VDS) Powershell Script Version 2 - Previously Created Volume Support + Mount Points + Bug Fixes @ Wednesday, September 24, 2008 1:15 AM

I got a lot of great feedback on the last VDS script - sounds like more than a few people have faced

Taylor Brown's Blog

# PDC Teaser – Attaching a VHD to A Virtual Machine @ Monday, October 13, 2008 6:55 PM

PDC is just around the corner now – I am giving the dry run of my talk tomorrow morning, so of course

Taylor Brown's Blog

Leave a Comment

(required) 
required 
(required) 
Page view tracker