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
0x80240022
When I tried this I start thinking about the function which will do the conversion and shows also description of the result code.
- # Name : Get-HResult.ps1
- # Author: David "Makovec" Moravec
- # Web : http://www.powershell.cz
- # Email : powershell.cz@googlemail.com
- #
- # Description: Finds meaning of HResult
- #
- # Version: 0.1
- # History:
- # v0.1 - (add) basic functionality
- #
- # Usage: Get-HResult -2145124318
- #
- #################################################################
- function Get-HResult {
- param (
- $HResult,
- $url = 'http://technet.microsoft.com/en-us/library/cc720442.aspx'
- )
- $calc = "0x{0:x}" -f [int]$HResult
- $lookFor = $calc+'.*?tr'
- $wc = New-Object system.net.webclient
- $wc.DownloadString($url) -match $lookFor | Out-Null
- $tmp = $matches[0]
- $searchString = '(?<ResCode>'+$calc+').*\<p\>(?<ResStr>.+?)\<.*\<p\>(?<Description>.+?)\</p.+'
- $tmp -match $searchString | Out-Null
- Write-Host "HResult: " $HResult
- Write-Host "Result Code: " $matches.ResCode
- Write-Host "Result String: " $matches.ResStr
- Write-Host "Description: " $matches.Description
- } #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.
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:
Okomentovat