locked
How to save byte array to registry? (Internet settings default connections) RRS feed

  • Question

  • I'm working with Internet settings.  The default settings are stored in a byte array.  I can read the object and use ExpandProperty to get the individual values.  I then modify some and want to save them back to the registry with Set-ItemProperty.

    My problem is the error trying to use an array as the value of the DefalultConnectionSettings attribute.  I think I need to create the byte array format it was originally, rather than a "plain" array.

    I "expanded" the original byte array into a "plain" array, made some updates and tried to save it as the "value" of Set-ItemProperty.  That generated the error, "A positional parameter cannot be found that accepts argument '0'.", which is one of the values in the array.

    Then I created a byte array, ( $baA1 = New-Object Byte[] 224 ), and updated each value based on the corresponding value of the "plain" array.

    When I use the byte array as the value for Set-ItemProperty, I get the same error.

    So I guess my question is, "How do I save/set this value?

    For reference, the registry item

    HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections

    Wednesday, April 1, 2020 6:18 PM

Answers

  • [byte[]]$bytes = 70,0,0,0,9,0,0,0,3,0,0,0,33,0,0,0,119,101,98,45,112,114,111,120,121,97,112,112,46,117,115,46,98,97,110,107,45,100,110,115,46,99,111,109,58,51,49,50,56,7,0,0,0,60,108,111,99,97,108,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,10,127,237,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    Set-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections' -Name DefaultConnectionSettings -Type Binary -Value $bytes
    

    $sb = {
        [byte[]]$bytes = 70,0,0,0,9,0,0,0,3,0,0,0,33,0,0,0,119,101,98,45,112,114,111,120,121,97,112,112,46,117,115,46,98,97,110,107,45,100,110,115,46,99,111,109,58,51,49,50,56,7,0,0,0,60,108,111,99,97,108,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,10,127,237,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        Set-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections' -Name DefaultConnectionSettings -Type Binary -Value $bytes
    }
    Invoke-Command -ScriptBlock $sb -Credential $Credential	-ComputerName $ComputerName
    


    \_(ツ)_/

    • Marked as answer by CBS3 Thursday, April 2, 2020 9:09 PM
    Thursday, April 2, 2020 4:59 PM

All replies

  • help Set-ItemProperty -online

     Set-ItemProperty $key -Name xxxxx -Value [byte[]]@(1,2,3) -Type Binary


    \_(ツ)_/

    Wednesday, April 1, 2020 6:31 PM
  • What command line are you actually running?  This works for me:


    set-itemproperty hkcu:\ binprop 0,1,2 -type binary

    • Edited by JS2010 Wednesday, April 1, 2020 10:25 PM
    Wednesday, April 1, 2020 10:19 PM
  • OK, here is the "fuller" version...

    I'm building a string, converting it to a script block and using it with Invoke-Command.  I'm confident in the Invoke-Command part as several other values are updating successfully.

    Here is the string being converted to ScriptBlock:

    Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections" -Name DefaultConnectionSettings -Type Binary -Value [byte[]]@(70,0,0,0,9,0,0,0,3,0,0,0,33,0,0,0,119,101,98,45,112,114,111,120,121,97,112,112,46,117,115,46,98,97,110,107,45,100,110,115,46,99,111,109,58,51,49,50,56,7,0,0,0,60,108,111,99,97,108,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,10,127,237,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

    Then, if interested or relevant, the Invoke-Command:

    	$sbCmdL  = [scriptblock]::Create( $strCmdL )
    
    	Invoke-Command	-ComputerName $ComputerName	`
    			-ScriptBlock  $sbCmdL		`
    			-Credential   $Credential

    The error generated is:

    A positional parameter cannot be found that accepts argument '70 0 0 0 9 0 ... 

    Thursday, April 2, 2020 4:30 PM
  • [byte[]]$bytes = 70,0,0,0,9,0,0,0,3,0,0,0,33,0,0,0,119,101,98,45,112,114,111,120,121,97,112,112,46,117,115,46,98,97,110,107,45,100,110,115,46,99,111,109,58,51,49,50,56,7,0,0,0,60,108,111,99,97,108,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,10,127,237,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    Set-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections' -Name DefaultConnectionSettings -Type Binary -Value $bytes
    

    $sb = {
        [byte[]]$bytes = 70,0,0,0,9,0,0,0,3,0,0,0,33,0,0,0,119,101,98,45,112,114,111,120,121,97,112,112,46,117,115,46,98,97,110,107,45,100,110,115,46,99,111,109,58,51,49,50,56,7,0,0,0,60,108,111,99,97,108,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,10,127,237,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        Set-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections' -Name DefaultConnectionSettings -Type Binary -Value $bytes
    }
    Invoke-Command -ScriptBlock $sb -Credential $Credential	-ComputerName $ComputerName
    


    \_(ツ)_/

    • Marked as answer by CBS3 Thursday, April 2, 2020 9:09 PM
    Thursday, April 2, 2020 4:59 PM
  • Yours is a case of command mode vs expression mode.  You would have needed an extra set of parentheses around the value.  I would just make a scriptblock variable in the first place, rather than try to convert a string to a scriptblock.  An array of integers would get converted automatically, if they were all in range (0-255).

    • Edited by JS2010 Thursday, April 2, 2020 5:24 PM
    Thursday, April 2, 2020 5:08 PM
  • THANK YOU!!!

    You got me to my final solution.

    I needed to build the byte array and define the registry path outside the script block definition, which is why I initially built the whole command as a string.  The last think I had to do was remember about the $Using directive to expand variables within the script block at execution time.  So finally i had:

    #define byte array. [byte[]$bytes = something #define path $DefaultConnectionSettingsPath = "HKCU:...." $sbCmdL = { Set-ItemProperty -Path Using:DefaultConnectionSettingsPath ` -Name DefaultConnectionSettings ` -Type Binary ` -Value $Using:bytes }#End

    Invoke-Command -ScriptBlock $sbCmdL `

    -Credential $Credential `

    -ComputerName $ComputerName



    • Edited by CBS3 Thursday, April 2, 2020 9:20 PM
    Thursday, April 2, 2020 9:09 PM