Page 1 of 1

having problems using parameters in a batch file

Posted: 05 Jan 2011 16:18
by nycgags
Normally when we spin up a new instance we need to add an alias on one of our boxes so we can easily connect to it through SSMS using SQL Server Configuration Manager.

I have written a batch file which adds the appropriate entry into the registry (it is actually two batch files, one for 32 bit and one for 64 bit). I am not sure how to get this to run with parameters though. I know %1 and %2 would be for the first two parameters, but when I run this, in the registry it actually puts %1 and %2 as the value pair.

if you hardcode hostname and IP Address in place of %1 and %2 the batch file works as expected:

Code: Select all

REGEDIT4

; @ECHO OFF
; CLS
; REGEDIT.EXE /S "%~f0"
; EXIT

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo]
"%1"="DBMSSOCN,%2,1433"

Re: having problems using parameters in a batch file

Posted: 05 Jan 2011 16:48
by aGerman
You should use reg.exe instead of regedit.exe in a batch file.

Code: Select all

@echo off
reg add "HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo" /v "%~1" /t "REG_SZ" /d "DBMSSOCN,%~2,1433" /f


Otherwise you have to redirect into a temporary .reg file to expand the values.

Regards
aGerman

Re: having problems using parameters in a batch file

Posted: 06 Jan 2011 12:54
by nycgags
awesome thanks, do I need to expand HKLM, or will reg translate it to: HKEY_LOCAL_MACHINE

Re: having problems using parameters in a batch file

Posted: 06 Jan 2011 13:27
by ChickenSoup
HKLM will expand to HKEY_LOCAL_MACHINE
HKCU will expand to HKEY_CURRENT_USER
HKU will expand to HKEY_USERS
etc.

Re: having problems using parameters in a batch file

Posted: 07 Jan 2011 09:35
by nycgags
I am in the Administrators group in Windows, yet I get this error when I try to execute:

ERROR: Access is denied.

What's weird is if I log in as Administrator (also part of the Administrators group) it succeeds...

Re: having problems using parameters in a batch file

Posted: 07 Jan 2011 14:46
by aGerman
Well, the message is clear. You have no access to HKEY_LOCAL_MACHINE with this account.

Hmm. And you didn't have this problems using regedit.exe?

If you are on Win7 then right click on the file and "run as administrator".

Regards
aGerman