pondělí 9. března 2009

Looking for HResult

In one of the last Hey, Scripting Guy! article is described how to install updates and check it's result codes. I was really surprised when Scripting guys told me: "We can use calc.exe" - OMG why not use converting directly in PowerShell?

PS C:\> "0x{0:x}" -f [int] -2145124318
0x80240022

When I tried this I start thinking about the function which will do the conversion and shows also description of the result code.

  1. # Name : Get-HResult.ps1
  2. # Author: David "Makovec" Moravec
  3. # Web : http://www.powershell.cz
  4. # Email : powershell.cz@googlemail.com
  5. #
  6. # Description: Finds meaning of HResult
  7. #
  8. # Version: 0.1
  9. # History:
  10. # v0.1 - (add) basic functionality
  11. #
  12. # Usage: Get-HResult -2145124318
  13. #
  14. #################################################################
  15. function Get-HResult {
  16. param (
  17. $HResult,
  18. $url = 'http://technet.microsoft.com/en-us/library/cc720442.aspx'
  19. )
  20. $calc = "0x{0:x}" -f [int]$HResult
  21. $lookFor = $calc+'.*?tr'
  22. $wc = New-Object system.net.webclient
  23. $wc.DownloadString($url) -match $lookFor | Out-Null
  24. $tmp = $matches[0]
  25. $searchString = '(?<ResCode>'+$calc+').*\<p\>(?<ResStr>.+?)\<.*\<p\>(?<Description>.+?)\</p.+'
  26. $tmp -match $searchString | Out-Null
  27. Write-Host "HResult: " $HResult
  28. Write-Host "Result Code: " $matches.ResCode
  29. Write-Host "Result String: " $matches.ResStr
  30. Write-Host "Description: " $matches.Description
  31. } #function

By default script return results for Windows Update Agent and has no checking for errors. It has parameter for providing different URL but I didn't try any. Usage is following:

PS C:\> Get-HResult -2145124351
Result Code: 0x80240001
Result String: WU_E_NO_SERVICE
Description: Windows Update Agent was unable to provide the service.

Source code will be available at PoSh Code when I'll be able to open it, now have troubles with connectivity.

Žádné komentáře: