Convert string to hex for registry

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
User avatar
syscouk
Posts: 56
Joined: Fri Nov 28, 2014 3:33 am

Convert string to hex for registry

Post by syscouk » Fri Apr 21, 2017 9:41 am

Hi,

I want to take a string of numbers read from an active directory attribute and convert it into an array such as "30,31,31" for "011"

How is this possible? I've got the below code to put $input into the registry as binary, but I need to read the attribute first and convert it to $input

$Path = "HKCU:\SOFTWARE\TestKey1"
$AttrName = "Test1"
$Input="30,00,31,00,31,00"
$hexified = $Input.Split(',') | ForEach-Object { "0x$_"}
$hexified

New-Item -Path HKCU:\SOFTWARE -Name Testkey1 -ErrorAction SilentlyContinue
Set-ItemProperty -Path $path -Name $AttrName -Value ([byte[]]$hexified) -verbose -ErrorAction 'Stop'


Thanks

User avatar
jvierra
Posts: 11347
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Convert string to hex for registry

Post by jvierra » Fri Apr 21, 2017 9:51 am

Do not use "$input". It is a reserved work.

Bytes are not hex. They are numbers.

$stringBytes="30,00,31,00,31,00"
$bytes = $stringBytes -spit ',' | %{ [int]$_}
New-ItemProperty -Path $path -Name $AttrName -Value $bytes -PropertyType REG_BINARY

User avatar
syscouk
Posts: 56
Joined: Fri Nov 28, 2014 3:33 am

Re: Convert string to hex for registry

Post by syscouk » Fri Apr 21, 2017 10:12 am

Thanks for that... but how do I convert "011" to "30,00,31,00,31,00" ? The AD attribute will be read as "011" so I need to get that into the bytes.

I should add that the script will be reading the attribute from different users which won't be just "011". Will be a multitude of numbers... The idea is to put whatever number is read from the attribute to a binary registry value (the reading from the attribute I've got working fine)

Thanks again

EDIT: I also get the error "New-ItemProperty : Could not bind parameter 'Type'. Could not convert "REG_BINARY" to "Microsoft.Win32.RegistryValueKind"" with that code

User avatar
jvierra
Posts: 11347
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Convert string to hex for registry

Post by jvierra » Fri Apr 21, 2017 10:22 am

What is "011"? It is a string.

Your question is a nit ambiguous since we have no idea what you are trying to do and your nomenclature is conflicted.

Are you asking how to convert a byte array into a string. Note that the registry encodes Unicode strings as byte arrays.

User avatar
jvierra
Posts: 11347
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Convert string to hex for registry

Post by jvierra » Fri Apr 21, 2017 10:33 am

Code: Select all

D:\scripts> $stringBytes="48,00,49,00,49,00"
D:\scripts> $bytes = $stringBytes -split ',' | %{ [int]$_}
D:\scripts> [System.Text.Encoding]::Unicode.GetString($bytes)
011
D:\scripts>

User avatar
jvierra
Posts: 11347
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Convert string to hex for registry

Post by jvierra » Fri Apr 21, 2017 10:36 am

To convert a string to a Unicode byte array:

[System.Text.Encoding]::Unicode.GetBytes('011')

User avatar
syscouk
Posts: 56
Joined: Fri Nov 28, 2014 3:33 am

Re: Convert string to hex for registry

Post by syscouk » Fri Apr 21, 2017 11:13 am

Hi and thanks.

Yeah as you guessed I'm not that great with powershell to be honest but I'll explain what i'm trying to do despite me not fully understanding the difference between bytes, hex, and binary in terms of writing to the registry:

Background: So Ricoh's printer codes are stored in the registry like so:

[HKEY_CURRENT_USER\Software\RICOH\JOBCODE\JCUserCode]
"\\\\serverxxx\\Main Ricoh MP C4503"=hex:30,00,31,00,31,00

This represents "011" in the user code box on the printer properties, but is actually shows as 0.1.1. in regedit. I want to set every users code in the registry to whatever is in extensionAttribute2 in active directory - this would mean taking "00000" from the attribute, converting it to "30,00,30,00,30,00,30,00,30,00 " (or 0.0.0.0.0. in regedit - the periods are required otherwise the printer doesn't recognise the code) and then writing that to the registry. I think I need to do the following:

1. Script will read "extensionAttribute2" from Active Directory and return the value to $userCode
2. $userCode will comprise of 5 numbers, i.e. 10976, 10244, 19437, etc.
3. Every user in AD will have a unique $userCode
4. I need $userCode which will be as a string, to be converted to the format above and written to the registry as a REG_BINARY

Does this make sense?

Thanks!
Last edited by syscouk on Fri Apr 21, 2017 11:31 am, edited 3 times in total.

User avatar
jvierra
Posts: 11347
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Convert string to hex for registry

Post by jvierra » Fri Apr 21, 2017 11:28 am

When dealing with hex specified values;

Convert the string to Unicode then convert it to hex a hex array.

[System.Text.Encoding]::Unicode.GetBytes('011')|%{[System.Convert]::ToString($_,16)}

User avatar
syscouk
Posts: 56
Joined: Fri Nov 28, 2014 3:33 am

Re: Convert string to hex for registry

Post by syscouk » Fri Apr 21, 2017 11:31 am

I'm not sure this needs a "conversion" per se, effectively I need to just split a 5 digit number for e.g. 12345, add a 3 before each number in the sequence, insert a comma, add 00 for a period and then move to next digit in the sequence: 12345 -----> 31,00,32,00,33,00,34,00,35,00

Would it still be better to convert?

EDIT: When I run [System.Text.Encoding]::Unicode.GetBytes('0.1.1.')|%{[System.Convert]::ToString($_,16)} I get 30 0 2e 0 31 0 2e 0 31 0 2e 0 which is wrong for the printer

User avatar
jvierra
Posts: 11347
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Convert string to hex for registry

Post by jvierra » Fri Apr 21, 2017 11:59 am

The code I posted will do exactly what you have asked.

If you keep changing the rules then it is unlikely that any answer will satisfy what you are trying to do. Why would you need to do this. Use the printer API to configure the settings. There is generally never any need to do any conversions. Just sore the correct values in AD or, better yet, Use Group Policy to configure printer settings.

Locked