shoober420
shoober420

Reputation: 3

Convert ASCII string to integer for registry key (WmiMonitorID.UserFriendlyName)

im trying to get a powershell script to query display EDID info and put it into a reg key. when trying to add the key, it says its not found, but the reg key tree exists. it turns out that all the variables must be in a integer format. the $Model variable isnt, and is a ascii string. how can i get this portion of the code to write the $Model variable so it adds the key?

foreach ($Monitor in @(Get-WmiObject -Namespace root\wmi -Class WmiMonitorID)) {
    $bigEndianInt32 = [Convert]::ToInt32(-join $Monitor.ManufacturerName[0..2].ForEach({[Convert]::ToString($_ - 64,2).PadLeft(5,'0')}),2)
    $Manufacturer = (Convert-BigEndianToLittleEndian $bigEndianInt32) -shr 16

    $ProductID = [int]::Parse(-join $Monitor.ProductCodeID[0..3].ForEach{[char]$_}, [System.Globalization.NumberStyles]::HexNumber)

    $Model = ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName[0..($Monitor.UserFriendlyNameLength -3)]) -split ' ')[0]
    # ...
}

complete script: https://github.com/shoober420/windows11-scripts/blob/main/AMDGPU_ColorDepth_BPC.ps1

i tried making all the variables strings, but that didnt work. they all need to be integers.

Upvotes: 0

Views: 105

Answers (2)

shoober420
shoober420

Reputation: 3

i was able to fix it by using this string

$Model = ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName)).Replace("$([char]0x0000)","")

using [char] is the best option

Upvotes: 0

js2010
js2010

Reputation: 27516

I guess there was a null in WmiMonitorID.UserFriendlyName (UInt16[]). I didn't have that problem. Even get-ciminstance gives you that unsigned int array for the userfriendlyname.

$Monitor = Get-CimInstance -Namespace root\wmi -Class WmiMonitorID
$Model = ([System.Text.Encoding]::ASCII.GetString(
  $Monitor.UserFriendlyName[0..($Monitor.UserFriendlyNameLength -3)]) -split 
  ' ')[0]

$Model.contains([char]0)

False


$Model

OptiPlex

Another workaround besides replacing the null in the string afterwards. (I've seen "-notmatch 0" elsewhere, which would be incorrect.)

$userfriendlyname = $monitor.userfriendlyname -ne 0
$Model = ([System.Text.Encoding]::ASCII.GetString(
  $userfriendlyname[0..($Monitor.UserFriendlyNameLength -2)]) -split 
  ' ')[0]

This is right out of the class docs. It has no methods.

gwmi WmiMonitorID -Namespace root\wmi |
  ForEach-Object {($_.UserFriendlyName -ne 0 | foreach {[char]$_}) -join ""}

OptiPlex 5270

Upvotes: 0

Related Questions