Blogger :
MSDN Blogs
All posts :
All posts by MSDN Blogs
Category :
SAPscript
Blogged date : 2008 Jun 19
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 ) 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) { Write-Error $Job.ErrorDescription Throw $Job.ErrorDescription } Write-Progress $Job.Caption "Completed" -Completed $TRUE } elseif ($_.ReturnValue -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 $a=$MethodQualifiers["ValueMap"].Value.Length while ($a -gt 0) { if ($MethodQualifiers["ValueMap"].Value[$a] -eq $_.ReturnValue) { Throw "ReturnCode: ", $_.ReturnValue, " ErrorMessage: '", $MethodQualifiers["Values"].Value[$a], "' - when calling $MethodName" } $a--; } Throw "ReturnCode: ", $_.ReturnValue, " ErrorMessage: 'MessageNotFound' - when calling $MethodName" } else { Throw "ReturnCode: ", $_.ReturnValue, "When calling $MethodName - for rich error messages provide classpath and method name." } } } #Do your actual scripting here… $VMManagementService = Get-WmiObject -Namespace root\virtualization -Class Msvm_VirtualSystemManagementService -ComputerName $HyperVHost #Will Generate an Error $VMManagementService.CreateVirtualSystemSnapshot("Not A Vm") | ProcessWMIJob $VMManagementService.PSBase.ClassPath "CreateVirtualSystemSnapshot"
|
Taylor Brown
Hyper-V Integration Test Lead
http://blogs.msdn.com/taylorb
