Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Simple loop overwriting registry key instead of creating new

$
0
0

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


Viewing all articles
Browse latest Browse all 15028

Trending Articles