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
Convert string to hex for registry
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Convert string to hex for registry
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
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
Re: Convert string to hex for registry
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
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
Re: Convert string to hex for registry
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.
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.
Re: Convert string to hex for registry
- 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>
Re: Convert string to hex for registry
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!
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 syscouk89 on Fri Apr 21, 2017 11:31 am, edited 3 times in total.
Re: Convert string to hex for registry
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)}
Convert the string to Unicode then convert it to hex a hex array.
[System.Text.Encoding]::Unicode.GetBytes('011')|%{[System.Convert]::ToString($_,16)}
Re: Convert string to hex for registry
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
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
Re: Convert string to hex for registry
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.
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.