Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - change odbc

Reply
 
Old 11-22-2007   #1 (permalink)
AHartman
Guest


 
 

change odbc

I recently moved some of my SQL databases to my new sql2005 box. Now I'm
looking for a way to send a script to people that will change there ODBC
connection to the new server.

any way to script this?


Thanks.


My System SpecsSystem Spec
Old 11-22-2007   #2 (permalink)
Shay Levi
Guest


 
 

Re: change odbc


The ODBC settings are stored in the registry. You can export the keys to
a reg file.


Under:
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources , you'll find
a key (name of dsn) for each SQL connection.

Under:
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI you'll see a key with sub values
corresponds to the value in the above key.


Export the two, open the reg files in notepad and make the neccessary changes
if needed. I recommend merging
the files into one reg file, then ask your users to import (double-click)
it on the destination server(s).


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> I recently moved some of my SQL databases to my new sql2005 box. Now
> I'm looking for a way to send a script to people that will change
> there ODBC connection to the new server.
>
> any way to script this?
>
> Thanks.
>

My System SpecsSystem Spec
Old 11-22-2007   #3 (permalink)
Shay Levi
Guest


 
 

Re: change odbc

I should have mention that DSNs under HKEY_LOCAL_MACHINE are system DSNs,
under the same path in HKEY_LOCAL_USER you can find
user DSNs, if any.

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> The ODBC settings are stored in the registry. You can export the keys
> to a reg file.
>
> Under: HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources ,
> you'll find a key (name of dsn) for each SQL connection.
>
> Under:
> HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI you'll see a key with sub
> values
> corresponds to the value in the above key.
> Export the two, open the reg files in notepad and make the neccessary
> changes
> if needed. I recommend merging
> the files into one reg file, then ask your users to import
> (double-click)
> it on the destination server(s).
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
Quote:

>> I recently moved some of my SQL databases to my new sql2005 box. Now
>> I'm looking for a way to send a script to people that will change
>> there ODBC connection to the new server.
>>
>> any way to script this?
>>
>> Thanks.
>>

My System SpecsSystem Spec
Old 11-23-2007   #4 (permalink)
Leo Tohill
Guest


 
 

RE: change odbc

Powershell includes a psDrive that makes the registry look like a file
system, so this is pretty easy to do. The following two-liner works:

$def = Get-childItem -path "hklm:\software\odbc\odbc.ini" -include
"mydatasourcename" -recurse
set-itemproperty -path $def.PsPath -name "Server" -value "myNewServerName"

Explanation:

The first line uses the HKLM: PSDrive to access the registry. I couldn't
figure out a more direct way to get the exact key that is needed,so I had to
specify a partial path (out to ODBC.Ini) and then use -include to specify the
datasource name, and -recurse was required too. I'm sure I'm missing a
better way, but this works. The result of this command is that $def
contains a reference to a registry object that describes that registry entry.
(Or $null, if the specified value doesn't exist.)

The second line uses the set-itemproperty cmdlet to set the property named
"Server" to the value "myNewServerName".

Change "myDataSourceName" and"myNewServerName" to whatever you are using.

HTH,

Leo




"AHartman" wrote:
Quote:

> I recently moved some of my SQL databases to my new sql2005 box. Now I'm
> looking for a way to send a script to people that will change there ODBC
> connection to the new server.
>
> any way to script this?
>
>
> Thanks.
>
>
My System SpecsSystem Spec
Old 11-23-2007   #5 (permalink)
AHartman
Guest


 
 

Re: change odbc

What if you only know the server name... they could use anything for a
datasource name.


"Leo Tohill" <LeoTohill@xxxxxx> wrote in message
news:1748AB4C-022F-482A-9135-9867F6D374CB@xxxxxx
Quote:

> Powershell includes a psDrive that makes the registry look like a file
> system, so this is pretty easy to do. The following two-liner works:
>
> $def = Get-childItem -path "hklm:\software\odbc\odbc.ini" -include
> "mydatasourcename" -recurse
> set-itemproperty -path $def.PsPath -name "Server" -value "myNewServerName"
>
> Explanation:
>
> The first line uses the HKLM: PSDrive to access the registry. I couldn't
> figure out a more direct way to get the exact key that is needed,so I had
> to
> specify a partial path (out to ODBC.Ini) and then use -include to specify
> the
> datasource name, and -recurse was required too. I'm sure I'm missing a
> better way, but this works. The result of this command is that $def
> contains a reference to a registry object that describes that registry
> entry.
> (Or $null, if the specified value doesn't exist.)
>
> The second line uses the set-itemproperty cmdlet to set the property named
> "Server" to the value "myNewServerName".
>
> Change "myDataSourceName" and"myNewServerName" to whatever you are using.
>
> HTH,
>
> Leo
>
>
>
>
> "AHartman" wrote:
>
Quote:

>> I recently moved some of my SQL databases to my new sql2005 box. Now I'm
>> looking for a way to send a script to people that will change there ODBC
>> connection to the new server.
>>
>> any way to script this?
>>
>>
>> Thanks.
>>
>>
My System SpecsSystem Spec
Old 11-23-2007   #6 (permalink)
Leo Tohill
Guest


 
 

Re: change odbc

If that's the case, you'd need to have the user input the dsn. Depending
upon how elaborate you want to be, you could simply use "read-host" to get an
entered value, or you could query and list them all, and have the user
select.


"AHartman" wrote:
Quote:

> What if you only know the server name... they could use anything for a
> datasource name.
>
>
> "Leo Tohill" <LeoTohill@xxxxxx> wrote in message
> news:1748AB4C-022F-482A-9135-9867F6D374CB@xxxxxx
Quote:

> > Powershell includes a psDrive that makes the registry look like a file
> > system, so this is pretty easy to do. The following two-liner works:
> >
> > $def = Get-childItem -path "hklm:\software\odbc\odbc.ini" -include
> > "mydatasourcename" -recurse
> > set-itemproperty -path $def.PsPath -name "Server" -value "myNewServerName"
> >
> > Explanation:
> >
> > The first line uses the HKLM: PSDrive to access the registry. I couldn't
> > figure out a more direct way to get the exact key that is needed,so I had
> > to
> > specify a partial path (out to ODBC.Ini) and then use -include to specify
> > the
> > datasource name, and -recurse was required too. I'm sure I'm missing a
> > better way, but this works. The result of this command is that $def
> > contains a reference to a registry object that describes that registry
> > entry.
> > (Or $null, if the specified value doesn't exist.)
> >
> > The second line uses the set-itemproperty cmdlet to set the property named
> > "Server" to the value "myNewServerName".
> >
> > Change "myDataSourceName" and"myNewServerName" to whatever you are using.
> >
> > HTH,
> >
> > Leo
> >
> >
> >
> >
> > "AHartman" wrote:
> >
Quote:

> >> I recently moved some of my SQL databases to my new sql2005 box. Now I'm
> >> looking for a way to send a script to people that will change there ODBC
> >> connection to the new server.
> >>
> >> any way to script this?
> >>
> >>
> >> Thanks.
> >>
> >>
>
>
My System SpecsSystem Spec
Old 11-23-2007   #7 (permalink)
Leo Tohill
Guest


 
 

RE: change odbc

Silly me, here's the way to write that first statement (use get-item instead
of get-childitem)

$def = Get-childItem -path "hklm:\software\odbc\odbc.ini\myDataSourceName"

Including your requirement to get the data source name from the user, you
could do this:


$def = $null
while (!$def)
{
$dsn = $null
while (!$dsn)
{
$dsn = read-host -prompt "Enter the data source name or ctrl-c to quit"
}
$def = Get-Item -path "hklm:\software\odbc\odbc.ini\$dsn" -erroraction
silentlycontinue
if (!$def)
{
out-host -input "There is no system data source named `"$dsn`""
}
}
set-itemproperty -path $def.PsPath -name "Server" -value "myNewServerName"




"Leo Tohill" wrote:
Quote:

> Powershell includes a psDrive that makes the registry look like a file
> system, so this is pretty easy to do. The following two-liner works:
>
> $def = Get-childItem -path "hklm:\software\odbc\odbc.ini" -include
> "mydatasourcename" -recurse
> set-itemproperty -path $def.PsPath -name "Server" -value "myNewServerName"
>
> Explanation:
>
> The first line uses the HKLM: PSDrive to access the registry. I couldn't
> figure out a more direct way to get the exact key that is needed,so I had to
> specify a partial path (out to ODBC.Ini) and then use -include to specify the
> datasource name, and -recurse was required too. I'm sure I'm missing a
> better way, but this works. The result of this command is that $def
> contains a reference to a registry object that describes that registry entry.
> (Or $null, if the specified value doesn't exist.)
>
> The second line uses the set-itemproperty cmdlet to set the property named
> "Server" to the value "myNewServerName".
>
> Change "myDataSourceName" and"myNewServerName" to whatever you are using.
>
> HTH,
>
> Leo
>
>
>
>
> "AHartman" wrote:
>
Quote:

> > I recently moved some of my SQL databases to my new sql2005 box. Now I'm
> > looking for a way to send a script to people that will change there ODBC
> > connection to the new server.
> >
> > any way to script this?
> >
> >
> > Thanks.
> >
> >
My System SpecsSystem Spec
Old 11-24-2007   #8 (permalink)
AHartman
Guest


 
 

Re: change odbc

cool... thanks!


"Leo Tohill" <LeoTohill@xxxxxx> wrote in message
news:6F6B5506-7B2F-4A65-998B-56C63ACFFD3B@xxxxxx
Quote:

> Silly me, here's the way to write that first statement (use get-item
> instead
> of get-childitem)
>
> $def = Get-childItem -path "hklm:\software\odbc\odbc.ini\myDataSourceName"
>
> Including your requirement to get the data source name from the user, you
> could do this:
>
>
> $def = $null
> while (!$def)
> {
> $dsn = $null
> while (!$dsn)
> {
> $dsn = read-host -prompt "Enter the data source name or ctrl-c to quit"
> }
> $def = Get-Item -path "hklm:\software\odbc\odbc.ini\$dsn" -erroraction
> silentlycontinue
> if (!$def)
> {
> out-host -input "There is no system data source named `"$dsn`""
> }
> }
> set-itemproperty -path $def.PsPath -name "Server" -value "myNewServerName"
>
>
>
>
> "Leo Tohill" wrote:
>
Quote:

>> Powershell includes a psDrive that makes the registry look like a file
>> system, so this is pretty easy to do. The following two-liner works:
>>
>> $def = Get-childItem -path "hklm:\software\odbc\odbc.ini" -include
>> "mydatasourcename" -recurse
>> set-itemproperty -path $def.PsPath -name "Server" -value
>> "myNewServerName"
>>
>> Explanation:
>>
>> The first line uses the HKLM: PSDrive to access the registry. I couldn't
>> figure out a more direct way to get the exact key that is needed,so I had
>> to
>> specify a partial path (out to ODBC.Ini) and then use -include to specify
>> the
>> datasource name, and -recurse was required too. I'm sure I'm missing a
>> better way, but this works. The result of this command is that $def
>> contains a reference to a registry object that describes that registry
>> entry.
>> (Or $null, if the specified value doesn't exist.)
>>
>> The second line uses the set-itemproperty cmdlet to set the property
>> named
>> "Server" to the value "myNewServerName".
>>
>> Change "myDataSourceName" and"myNewServerName" to whatever you are using.
>>
>> HTH,
>>
>> Leo
>>
>>
>>
>>
>> "AHartman" wrote:
>>
Quote:

>> > I recently moved some of my SQL databases to my new sql2005 box. Now
>> > I'm
>> > looking for a way to send a script to people that will change there
>> > ODBC
>> > connection to the new server.
>> >
>> > any way to script this?
>> >
>> >
>> > Thanks.
>> >
>> >
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
ODBC drivers Drivers
Odbc General Discussion
ODBC crashed General Discussion
SQL Select in ODBC Vista General
odbc Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46