QueryVersionAndStatus.ps1 PowerShell script

Save the code contained in this topic as a PowerShell script (.ps1 file). Run the QueryVersionAndStatus.ps1 PowerShell (PS) script to query the current firmware version, last attempt firmware version and last attempt status, as described in Validating Windows UEFI Firmware Update Platform Functionality.

#
# QueryVersionAndStatus.ps1
#      Allows querying the current firmware version of a guid and the
#      LastAttemptStatus
#

new-variable -name FIRMWARERESOURCES_REGKEY_PATH -value "HKLM:\System\CurrentControlSet\Control\FirmwareResources\" -option Constant
new-variable -name LAST_ATTEMPT_STATUS_REGKEY -value "LastAttemptStatus" -option Constant
new-variable -name LAST_ATTEMPT_VERSION_REGKEY -value "LastAttemptVersion" -option Constant

##############################################################################
#
# Main routine.
# CmdLineArg - Array containing the command line arguments
#
##############################################################################
function Main($CmdLineArg) {
  $currentFwVersion = 0
  $currentFwVersionInHex = 0
  $lastAttemptFwVersion = 0
  $lastAttemptFwVersionInHex = 0
  $lastAttemptStatus = 0
  $lastAttemptStatusInHex = 0

  if ($CmdLineArg.Count -eq 0) {
    Write-Host -ForegroundColor Red -BackgroundColor Black "Expecting GUID of the EFI System Resource Entry (ESRE)"
    Write-Host "Usage: .\QueryVersionAndStatus.ps1 <guid>"
    Write-Host "`tQuery the current firmware version and Last Attempt Status for the ESRE entry with the given GUID"
    Write-Host "`tExample: .\QueryVersionAndStatus.ps1 6bd2efb9-23ab-4b4c-bc37-016517413e9a"
    return
  }

  if ($CmdLineArg[0] -eq "-?") {
    Write-Host "Usage: QueryVersionAndStatus.ps1 <guid>"
    Write-Host "`tQuery the current firmware version and Last Attempt Status for the ESRE entry with the given GUID"
    Write-Host "`tExample: .\QueryVersionAndStatus.ps1 6bd2efb9-23ab-4b4c-bc37-016517413e9a"
    return
  }

  # First argument is the GUID
  $guid = $CmdLineArg[0]

  # Validate the GUID
  if ($guid -notmatch "[0-9a-fA-F]{8}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{12}") {
      Write-Host -ForegroundColor Red -BackgroundColor Black "Incorrect Format for GUID{$guid}"
      Write-Host -ForegroundColor Red -BackgroundColor Black "Expected GUID Format: [0-9a-fA-F]{8}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{12}"
      Write-Host -ForegroundColor Red -BackgroundColor Black "Sample GUID: 6bd2efb9-23ab-4b4c-bc37-016517413e9a"
      return
  }

  Write-Host "Querying Firmware version and status information for the GUID: $guid"
  $query = GetCurrentFirmwareVesrion ($guid) ([REF]$currentFwVersion)
  if ($query -eq $FALSE) {
    Write-Host -ForegroundColor Red -BackgroundColor Black "Unable to query the current firmware version"
    return
  }
  $currentFwVersionInHex = "{0:X}" -f $currentFwVersion
  Write-Host "Current Firmware Version: $currentFwVersion (0x${currentFwVersionInHex})"

  $query = GetLastAttemptVesrion ($guid) ([REF]$lastAttemptFwVersion)
  if ($query -eq $FALSE) {
    Write-Host -ForegroundColor Red -BackgroundColor Black "Unable to query the last attempt firmware version"
    return
  }
  $lastAttemptFwVersionInHex = "{0:X}" -f $lastAttemptFwVersion
  Write-Host "Last Attempt Version: $lastAttemptFwVersion (0x${lastAttemptFwVersionInHex})"

  $query = GetLastAttemptStatus ($guid) ([REF]$lastAttemptStatus)
  if ($query -eq $FALSE) {
    Write-Host -ForegroundColor Red -BackgroundColor Black "Unable to query the last attempt status"
    return
  }
  $lastAttemptStatusInHex = "{0:X}" -f $lastAttemptStatus
  Write-Host "Last Attempt Status: $lastAttemptStatus (0x${lastAttemptStatusInHex})"
}

##############################################################################
#
# Constructs and returns the Firmware Resources registry key for the GUID.
# Guid - The GUID for which the resource registry key needs to be retrieved
# @Returns Firmware Resources registry key. $NULL on any error.
#
##############################################################################
function GetFirmwareResourceKeyForGuid($Guid) {
  if ($Guid -eq $NULL) {
    return $NULL
  }
  $resourceGuidRegKey = $FIRMWARERESOURCES_REGKEY_PATH + '{' + $Guid + '}'
  return $resourceGuidRegKey
}

##############################################################################
#
# Get the current firmware version for the given GUID. The current firmware
# version for a firmware resource device is stored in its Hardware ID.
# The HW ID is of the form: UEFI\RES_{<GUID>}&REV_XXXX
# where XXXX is the current firmware version in Hex.
# Guid - The GUID for which the firmware version has to be queried.
# [REF]FwVersionByRef - Current firmware version that is returned by reference
# @Returns - $TRUE if the query for firmware version was successful,
#   $FALSE otherwise
#
##############################################################################
function GetCurrentFirmwareVesrion($Guid, [REF]$FwVersionByRef) {

  if ( ($Guid -eq $NULL) -Or
       ($FwVersionByRef.value -eq $NULL)) {
    Write-Host -ForegroundColor Red -BackgroundColor Black "Unexpected NULL Argument(Guid) or (FwVersionByRef)"
    return $FALSE
  }

  $hwId = "UEFI\\RES_{$Guid}"
  $pnpProperties = gwmi win32_pnpentity | where-object {$_.HardwareID -match $hwId}
  if (!$?) {
    Write-Host -ForegroundColor Red -BackgroundColor Black $Error[0]
    return $FALSE
  }
  if ($pnpProperties -eq $NULL) {
    Write-Host -ForegroundColor Red -BackgroundColor Black "No Device found with matching Hardware ID{$hwId}"
    return $FALSE
  }

  # Firmware resource devices have two Hardware IDs. One with the revision
  # and one without. We are interested the one with the Revision and that is the
  # first one.
  $hwIdWithRev = $pnpProperties.HardwareID[0]
  $devRevStr = $hwIdWithRev.SubString($hwIdWithRev.IndexOf("&REV_")+("&REV_").Length, $hwIdWithRev.Length - $hwIdWithRev.IndexOf("&REV_") - ("&REV_").Length)
  $devRev = [Convert]::ToInt32("$devRevStr", 16)
  $FwVersionByRef.value = $devRev
  return $TRUE

}

##############################################################################
#
# Get the last attempt firmware version for the given GUID. The last attempt firmware
# version for a firmware resource device is stored in its Firmware Resource
# registry keys under
# HKLM\System\CurrentControlSet\FirmwareResources\{<GUID>\ in the key
# "LastAttemptVersion"
# Guid - The GUID for which the firmware version has to be queried.
# [REF]FwVersionByRef - Expected firmware version that is returned by reference
# @Returns - $TRUE if the query for firmware version was successful,
#   $FALSE otherwise
#
##############################################################################
function GetLastAttemptVesrion($Guid, [REF]$FwVersionByRef) {

  if ( ($Guid -eq $NULL) -Or
       ($FwVersionByRef.value -eq $NULL)) {
    Write-Host -ForegroundColor Red -BackgroundColor Black "Unexpected NULL Argument(Guid) or (FwVersionByRef)"
    return $FALSE
  }

  $resourceGuidRegKey = GetFirmwareResourceKeyForGuid($Guid)
  $expectedVersion = Get-ItemProperty $resourceGuidRegKey -Name $LAST_ATTEMPT_VERSION_REGKEY
  if (!$?) {
    Write-Host -ForegroundColor Red -BackgroundColor Black $Error[0]
    return $FALSE
  }
  $FwVersionByRef.Value = ${expectedVersion}.${LAST_ATTEMPT_VERSION_REGKEY}
  return $TRUE

}

##############################################################################
#
# Get the last attempt status for the given GUID.
# The last attempt status for a firmware resource device is stored in its
# Firmware Resource registry keys under HKLM\System\CurrentControlSet\FirmwareResources\{<GUID>\
# in the key "LastAttemptStatus".
# Guid - The GUID for which the firmware version has to be queried.
# [REF]FwVersionByRef - Expected firmware version that is returned by reference
# @Returns - $TRUE if the query for firmware version was successful,
#   $FALSE otherwise
#
##############################################################################
function GetLastAttemptStatus($Guid, [REF]$LastAttemptStatusByRef) {

  if ( ($Guid -eq $NULL) -Or
       ($LastAttemptStatusByRef.value -eq $NULL)) {
    Write-Host -ForegroundColor Red -BackgroundColor Black "Unexpected NULL Argument(Guid) or (LastAttemptStatusByRef)"
    return $FALSE
  }

  $resourceGuidRegKey = GetFirmwareResourceKeyForGuid($Guid)
  $expectedVersion = Get-ItemProperty $resourceGuidRegKey -Name $LAST_ATTEMPT_STATUS_REGKEY
  if (!$?) {
    Write-Host -ForegroundColor Red -BackgroundColor Black $Error[0]
    return $FALSE
  }
  $LastAttemptStatusByRef.Value = ${expectedVersion}.${LAST_ATTEMPT_STATUS_REGKEY}
  return $TRUE

}

$ErrorActionPreference = "SilentlyContinue"
Main($args)
# SIG # Begin signature block
# MIIO7wYJKoZIhvcNAQcCoIIO4DCCDtwCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCY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Axtxcu1W3+XDqr7cl2wSPNeJbi0u6H2
# ney1RWTMZ1sCbJ0dx/lmb/0sUlk+Xq28gTdZD30ZFdzYro0k/DGCAjowggI2AgEB
# MIGQMIGBMRMwEQYKCZImiZPyLGQBGRYDY29tMRkwFwYKCZImiZPyLGQBGRYJbWlj
# cm9zb2Z0MRQwEgYKCZImiZPyLGQBGRYEY29ycDEXMBUGCgmSJomT8ixkARkWB3Jl
# ZG1vbmQxIDAeBgNVBAMTF01TSVQgVGVzdCBDb2RlU2lnbiBDQSAzAgoVedLHAAAA
# X9x1MA0GCWCGSAFlAwQCAQUAoHwwEAYKKwYBBAGCNwIBDDECMAAwGQYJKoZIhvcN
# AQkDMQwGCisGAQQBgjcCAQQwHAYKKwYBBAGCNwIBCzEOMAwGCisGAQQBgjcCARUw
# LwYJKoZIhvcNAQkEMSIEIMjNoIyg2MGG+LFOqGSBLXVkS+8WthPViNe0xt7N/Zha
# MA0GCSqGSIb3DQEBAQUABIIBAEDSPp6JtrDpRQOwvazx8NqmcIF2A2HFnnp1h+Lp
# aHUAyl5M07iri/MIqW64RbREOdbCh7yAwFqw8m1PTHwk42xMW+mQPxPvr2gyk45N
# k1YWCXST0fjutwrZKY3w7Q4sSoas0F5KHkb9DPncb8aM+rrlyBhvo7aZcEyc/mXq
# 672CR2fVfTniJO6FP/GjK5hmtvlmcMnbtO9ztKgpc7E6Z1hSns0KNyXZdRdXC3EX
# tsJ7grMN9gy+RtkTBfiQBCDqdytnloZIk6DUyof32SC6BG88yllmYKT3Y6WsisVM
# vQQdvRMzv61bF54XJvC0I6OkpCEaYF9XR5E02CK4XdUch+g=
# SIG # End signature block

Validating Windows UEFI Firmware Update Platform Functionality