none
Script to append HKLM Registry value not working RRS feed

  • General discussion

  • Good morning

    I have the below script which should amend values in HKLM
    it should read the value from strvaluename and then this is the oldvalue, then set a new string value which prefixes with the strvalue

    Const HKEY_LOCAL_MACHINE = &H80000002
    strComputer = "."
    Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
    strKeyPath = "SOFTWARE\Wow6432Node\Interwoven\Worksite\Client\FileSite\Commands\Workspace\"
    strValueName = "Commands"
    strValue = ",-,printBarcode.printBC@Print Barcode"
    objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, oldValue
    newValue = oldValue & "," & strValue
    objRegistry.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, newValue

    This works fine as HKCU as shown in the below script
    Const HKEY_CURRENT_USER = &H80000001
    strComputer = "."
    Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
    strKeyPath = "Software\Microsoft\Internet Explorer\International\"
    strValueName = "AcceptLanguage"
    strValue = "en-GB;Q=0.5"
    objRegistry.GetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, oldValue
    newValue = oldValue & "," & strValue
    objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, newValue

    can anyone advise, where I am going wrong? this is run as an admin so should work fine I believe?

    Regards

    Chris

    • Changed type Bill_Stewart Friday, July 7, 2017 6:21 PM
    • Moved by Bill_Stewart Friday, July 7, 2017 6:21 PM Unanswerable drive-by question
    Monday, May 15, 2017 8:32 AM

All replies

  • What is the full error you are getting?

    You would find this easier in PowerShell:

    $keyPath = 'HKLM:\SOFTWARE\Wow6432Node\Interwoven\Worksite\Client\FileSite\Commands\Workspace'
    if ($item = Get-ItemProperty $key -Name Commands -ErrorAction 0) {
    	$oldvalue = $item.Commands
    	Set-ItemProperty $key -Name Commands -Value "$oldValue,-,printBarcode.printBC@Print Barcode"
    } else {
    	Write-Host 'Key/Item not found'
    }


    \_(ツ)_/


    • Edited by jrv Monday, May 15, 2017 1:15 PM
    Monday, May 15, 2017 1:13 PM
  • Good morning

    thank you for the very fast response :)

    it just doesn't do anything, run from an elevated command prompt

    yet if I change it from HKLM to HKCU it works fine every time?

    it needs to be VBScript as that's what they would like, which is why I'm a tad stuck :)

    any ideas as I haven't a clue why it wont work

    regards

    Tuesday, May 16, 2017 8:20 AM
  • You must be an administrator at an elevated prompt to write to HKLM.  Many keys are not writable in HKLM even for admins.


    \_(ツ)_/

    Tuesday, May 16, 2017 2:35 PM
  • Hi It is an elevated command prompt I am Using with no luck Regards
    Tuesday, May 16, 2017 2:37 PM
  • So, what's your question then?

    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 16, 2017 2:48 PM
  • Hello Pretty straight forward question Am I missing something in my script that needs adding for Hklm you must be able to change or append hklm keys but I aren't sure why it's not working Regards
    Tuesday, May 16, 2017 2:50 PM
  • Try to explain in plain language what you want to do, not how you think it needs to be done.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 16, 2017 3:37 PM
  • Hi Bill I thought I'd done that in the first line of my first post I want to append a value with an additional prefix So it will take the original value from a string value in HKLM and add a prefix to it For example string value is test I want to append this to test;user2 I can do this under hkcu but hklm does Not work Regards
    Tuesday, May 16, 2017 3:43 PM
  • I suggested that you do not have access t the key.  You need to check that.  This is done by using correct scripting methods and testing the return values

    strKeyPath = "SOFTWARE\Wow6432Node\Interwoven\Worksite\Client\FileSite\Commands\Workspace\" 
    strValueName = "Commands" 
    strValue = ",-,printBarcode.printBC@Print Barcode"
    
    Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") 
    returnValue = objRegistry.GetStringValue(&H80000002, strKeyPath, strValueName, oldValue)
    if returnValue = 0 Then
    	newValue = oldValue & "," & strValue
    	returnValue = objRegistry.SetStringValue(&H80000002, strKeyPath, strValueName, newValue)
    	If returnValue <> 0 Then
    		WScript.Echo  "Set failed with:" & returnValue
    	End If
    Else
    	WScript.Echo  "Get failed with:" & returnValue
    End If
    Set/Get returns 0/1.  0 is "success" and 1 is "failed".


    \_(ツ)_/



    • Edited by jrv Tuesday, May 16, 2017 4:08 PM
    Tuesday, May 16, 2017 3:56 PM
  • Also note that a return value of 5 indicates "Access denied".

    net helpmsg 5


    \_(ツ)_/


    • Edited by jrv Tuesday, May 16, 2017 4:25 PM
    Tuesday, May 16, 2017 4:24 PM