none
Simple loop overwriting registry key instead of creating new RRS feed

  • Question

  • I've created a ps script to create some registry keys for me, and I've run into a snag. I've dumbed it down a lot to simplify for here, and show the results I'm getting. I have a while loop, that is looping 3 times, which should create a key name that is different based on the number in the loop being part of the name. I echoed the variables I'm using so we can test that they are what they should be. However, the first two "iterations" of the loop are either not going to the registry (doubtful?), or they are being overwritten by each subsequent iteration, as I only see the results of the third one. Here is my script:

    Param(
        [string]$ODBCName,
        [string]$Vno,
        [int]$DBCount,
        [int]$DBCounter
    )

    $DBCount = 3

    while ($DBCounter -lt $DBCount)
    {
    echo 'Dbcount =' $DBCount
    echo 'dbcounter =' $DBCounter
     $DBCounter = $DBCounter + 1
     $VNo = "v" + $DBCounter

    $ODBCName = $VNo
    echo "odbc name =" $ODBCName

    New-Item -Path HKCU:\SOFTWARE\ODBC\ODBC.INI -Name 'ODBC Data Sources' -Force

    New-ItemProperty -Path HKCU:\SOFTWARE\ODBC\ODBC.INI\'ODBC Data Sources' -Name $ODBCName -Value 'ODBC Client Interface' -Force
    }

    Here is the output from the script:

    PS C:\Windows\system32> C:\Users\cloud.administrator.ACCELLOSONDEMAN\Documents\test.ps1
    Dbcount =
    3
    dbcounter =
    0
    odbc name =
    v1


        Hive: HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI


    Name                           Property                                                                                                                                                     
    ----                           --------                                                                                                                                                     
    ODBC Data Sources                                                                                                                                                                           

    v1           : ODBC Client Interface
    PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources
    PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI
    PSChildName  : ODBC Data Sources
    PSDrive      : HKCU
    PSProvider   : Microsoft.PowerShell.Core\Registry

    Dbcount =
    3
    dbcounter =
    1
    odbc name =
    v2
    ODBC Data Sources                                                                                                                                                                           

    v2           : ODBC Client Interface
    PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources
    PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI
    PSChildName  : ODBC Data Sources
    PSDrive      : HKCU
    PSProvider   : Microsoft.PowerShell.Core\Registry

    Dbcount =
    3
    dbcounter =
    2
    odbc name =
    v3
    ODBC Data Sources                                                                                                                                                                           

    v3           : ODBC Client Interface
    PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources
    PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI
    PSChildName  : ODBC Data Sources
    PSDrive      : HKCU
    PSProvider   : Microsoft.PowerShell.Core\Registry

     Yet in the registry, I see only an entry of "v3 of type Reg_sz with value of ODBC Client Interface".

    What am I doing wrong? Why do I have only one entry, instead of 3?

    Thanks in advance!

    mpleaf

    Tuesday, February 18, 2014 7:15 PM

Answers

  • This line is always rewriting the key so only the last iteration will show.

    This is the template for what it appears that you are trying to do.

    $ODBCName='TestODBC'
    
    $keypath= 'HKCU:\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources'
    if(Test-Path $keypath){
        Write-Host 'Path exists' -ForegroundColor green
    }else{
        New-Item -Path HKCU:\SOFTWARE\ODBC\ODBC.INI -Name 'ODBC Data Sources' -Force
    }
    
    if(Get-ItemProperty -Path $keypath -Name $ODBCName -ea 0){
        Write-Host 'Key already exists' -ForegroundColor green
    }else{
        New-ItemProperty -Path $keypath -Name $ODBCName -Value 'ODBC Client Interface'
    }


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, February 18, 2014 9:11 PM
    • Marked as answer by mpleaf Tuesday, February 18, 2014 11:14 PM
    Tuesday, February 18, 2014 9:11 PM

All replies

  • This line is always rewriting the key so only the last iteration will show.

    This is the template for what it appears that you are trying to do.

    $ODBCName='TestODBC'
    
    $keypath= 'HKCU:\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources'
    if(Test-Path $keypath){
        Write-Host 'Path exists' -ForegroundColor green
    }else{
        New-Item -Path HKCU:\SOFTWARE\ODBC\ODBC.INI -Name 'ODBC Data Sources' -Force
    }
    
    if(Get-ItemProperty -Path $keypath -Name $ODBCName -ea 0){
        Write-Host 'Key already exists' -ForegroundColor green
    }else{
        New-ItemProperty -Path $keypath -Name $ODBCName -Value 'ODBC Client Interface'
    }


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, February 18, 2014 9:11 PM
    • Marked as answer by mpleaf Tuesday, February 18, 2014 11:14 PM
    Tuesday, February 18, 2014 9:11 PM
  • I don't think so. This appears to only write out if it already doesn't exist. I'm trying to write out 3 values, but am only getting one of them, the last one. I need all three to write out from the loop.

    mpleaf

    Tuesday, February 18, 2014 10:22 PM
  • I don't think so. This appears to only write out if it already doesn't exist. I'm trying to write out 3 values, but am only getting one of them, the last one. I need all three to write out from the loop.

    mpleaf

    So it will write three values.  You just need to understand why your original code would only write one.  I told you why that was happening.  THink about it.  You will see the mistake in your logic eventually.  I can show you but I cannot make you understand.


    ¯\_(ツ)_/¯

    Tuesday, February 18, 2014 11:01 PM
  • By the way, the code I posted is not intended as a solution but as a demonstration of why your code won't work.  It also shows one of many ways to overcome this.

    ¯\_(ツ)_/¯

    Tuesday, February 18, 2014 11:03 PM
  • I'm an idiot! I apologize, I didn't see a line in your original post:

    "This line is always rewriting the key so only the last iteration will show."

    You are correct. I am recreating the path inside the loop, when I want to do that outside the loop. If I put it before the loop, I am all set!

    Thanks for the guidance!

    mpleaf

    Tuesday, February 18, 2014 11:13 PM
  • I am still not sure what you are trying to do but not putting that in the loop will help.

    ¯\_(ツ)_/¯

    Tuesday, February 18, 2014 11:32 PM