Custom registry script/binary problems

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Troy Oosterwijk Troy Oosterwijk 1 year, 9 months ago.

  • Author
    Posts
  • #21579
    Profile photo of Troy Oosterwijk
    Troy Oosterwijk
    Participant

    Hey all,

    I've got some issues with a custom made script, which simplifies adjusting registry values.
    First let me show you the function:

    function Create-Location
    {
    [CmdletBinding[]]
        Param[
        # Param1 help description 
        [Parameter[Mandatory=$true]]  
        [String]$LiteralPath,
        # Param2 help description
        [Parameter[Mandatory=$false]]  
        [String]$Name,
        # Param3 help description 
        [String]$Value,
        # Param4 help description 
        [String]$PropertyType
        ]
    
        if[![Test-Path -Path $LiteralPath]] 
        {
        $Caption = "Please confirm:"    
        $Message = "$Literalpath does not exist. Do you want to create it?"
        [int]$DefaultChoice = 0
        $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Do the job."
        $No = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Do not do the job."
        $Options = [System.Management.Automation.Host.ChoiceDescription[]][$Yes, $No]
        $ChoiceRTN = $host.ui.PromptForChoice[$Caption,$Message, $Options,$DefaultChoice]
        if [ $choiceRTN -ne 1 ] {New-Item -Path $LiteralPath -Force}
        else {}
        }
    
        If[$Name]
        {
         Write-Host "Set-Location -LiteralPath $LiteralPath"
         Set-Location -LiteralPath $LiteralPath
         Write-Host "New-ItemProperty -Path `".\`" -Name $Name -Value $Value -PropertyType $PropertyType –Force"
         New-ItemProperty -Path '.\' -Name $Name -Value $Value -PropertyType $PropertyType –Force
         }
    
        $LiteralPath = $null
        $Name = $null
        $Value = $null
        $PropertyType = $null
     }

    Now, with this function, I can easily adjust bulk values of registry data like this:

    Create-Location -LiteralPath 'HKCU:\Software\Something' -Name 'Something' -Value 'Something' -PropertyType "DWord"

    And this is working fine for most registry entries and values, except for this one:

    Create-Location -LiteralPath 'HKCU:\Control Panel\Desktop' -Name 'UserPreferencesMask' -PropertyType Binary -Value [[byte[]] [0x90,0x12,0x03,0x80,0x0A,0x10,0x00,0x00]]

    No matter what I've tried, it just won't accept the byte value with the use of the Create-Location function.
    Strangely enough, when I use it the "old-fashion" way, it works.

    Set-Location -LiteralPath 'HKCU:\Control Panel\Desktop'
    New-ItemProperty -Path '.' -Name 'UserPreferencesMask' -Value [[byte[]] [0x90,0x12,0x03,0x80,0x0A,0x10,0x00,0x00]] -PropertyType "Binary" -Force

    I've tried putting the value into a variable in numerous ways, but it's only accepted with the New-ItemProperty method.
    At this point, Ive ran out of ideas...

    Is there some mistake I'm making in the format of the function?
    If anyone here knows a solution, I'd be happy to hear it from you!

    Thanks and regards,

    T.O.

  • #21581
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Troy,

    If you remove the [String] data type attribute of your $Value parameter in the Param block it should work as expected.

    Before:

    Param (
      # Param1 help description 
      [Parameter(Mandatory=$true)]  
      [String]$LiteralPath,
      # Param2 help description
      [Parameter(Mandatory=$false)]  
      [String]$Name,
      # Param3 help description 
      [String]$Value,
      # Param4 help description 
      [String]$PropertyType
    )
    

    After:

    Param (
      # Param1 help description 
      [Parameter(Mandatory=$true)]  
      [String]$LiteralPath,
      # Param2 help description
      [Parameter(Mandatory=$false)]  
      [String]$Name,
      # Param3 help description 
      $Value,
      # Param4 help description 
      [String]$PropertyType
    )
    
  • #21584
    Profile photo of Stephen Owen
    Stephen Owen
    Participant

    To piggy back on what Daniel is saying, you're specifying a [string], which is looking for one single value/object. Either remove [string] or use [string[]] instead, which allows for multiple objects.

  • #22641
    Profile photo of Troy Oosterwijk
    Troy Oosterwijk
    Participant

    Thanks for the reply all!
    The script is now working as it should.

You must be logged in to reply to this topic.