Home > Mobile >  How can i get the gpu model name from inside the brackets?
How can i get the gpu model name from inside the brackets?

Time:08-24

i want to get just the model name of the gpu by searching for the "$deviceid".

So this is what i have at the moment:

Install-Module PSParseHtml

$deviceid = "0x1f02"
$Url = 'https://envytools.readthedocs.io/en/latest/hw/pciid.html#gpu'
$AllTables = ConvertFrom-HtmlTable -Url $Url
$AllTables | Where-Object {$_."device id" -match $deviceid}

My output is this:

device id product                        
--------- -------                        
0x1f02    TU106 [GeForce RTX 2070]       
0x1f07    TU106 [GeForce RTX 2070]       
0x1f08    TU106 [GeForce RTX 2060]       
0x1f10    TU106 [GeForce RTX 2070 Mobile]
0x1f11    TU106 [GeForce RTX 2060 Mobile]
0x1f50    TU106 [GeForce RTX 2070 Mobile]
0x1f51    TU106 [GeForce RTX 2060 Mobile]

How can i get now as output only the gpu model name "GeForce RTX 2070" with the matching device id "0x1f02"

Thank you

CodePudding user response:

Your question has two distinct aspects:

  • A question that is specific to the PSParseHTML module's ConvertFrom-HtmlTable cmdlet:

    • ConvertFrom-HtmlTable, outputs whole arrays in your case, so that $AllTables is in effect a nested array, whereas you want to filter by the individual elements of those nested arrays.

    • To force these nested arrays to be enumerated, so that Where-Object can act on individual objects, as usual, you can simply pipe to Write-Output:

      # Thanks to Write-Output, outputs *only* the object (table row)
      # with device ID $deviceID. 
      # Note the use of -eq instead of -match
      # (-match is only needed for regex matching).
      $AllTables | 
        Write-Output |  # Enumerate arrays
        Where-Object { $_.'device id' -eq $deviceid }
      
  • A general question about how to select and transform properties from given input objects - see below.


In order to select a subset of properties and/or transform the values of the properties of input objects, use Select-Object. In order to transform property values, use it with calculated properties. The result will be [pscustomobject] instances that have the desired properties and values.

$sampleInput = [pscustomobject] @{
  'device id' = '0x1f02'
  product = 'TU106 [GeForce RTX 2070]'
}

# Use a calculated property to select and transform the 'product' value,
# and select the 'device id' property as-is.
$sampleInput |
  Select-Object @{
      Name='product';
      Expression={ ($_.product -split '[][]')[1] }
    }, 
    'device id'

The regex-based -split operator is used to split each product name by [ and ]; the 2nd element (index 1) of the resulting array contains the substring of interest. Alternatively, you could use the regex-based -replace operator: $_.product -replace '^. \[(. ?)\]$', '$1'

Output:

product          device id
-------          ---------
GeForce RTX 2070 0x1f02

Note: If you don't actually need to construct new objects and are simply looking for the formatted display shown above, you can substitute Format-Table for Select-Object; however, note that such output is then truly only usable for display purposes.

  • Related