次の方法で共有


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
https://blogs.msdn.com/taylorb

clip_image001

Comments

  • Anonymous
    June 18, 2008
    PingBack from http://wordnew.acne-reveiw.info/?p=243

  • Anonymous
    July 06, 2008
    The comment has been removed

  • Anonymous
    September 19, 2008
    This is by far the longest post both in length and the time it took to figure it out I have ever done

  • Anonymous
    September 23, 2008
    I got a lot of great feedback on the last VDS script - sounds like more than a few people have faced

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