having problems using parameters in a batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nycgags
Posts: 3
Joined: 05 Jan 2011 16:07

having problems using parameters in a batch file

#1 Post by nycgags » 05 Jan 2011 16:18

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"

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: having problems using parameters in a batch file

#2 Post by aGerman » 05 Jan 2011 16:48

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

nycgags
Posts: 3
Joined: 05 Jan 2011 16:07

Re: having problems using parameters in a batch file

#3 Post by nycgags » 06 Jan 2011 12:54

awesome thanks, do I need to expand HKLM, or will reg translate it to: HKEY_LOCAL_MACHINE

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: having problems using parameters in a batch file

#4 Post by ChickenSoup » 06 Jan 2011 13:27

HKLM will expand to HKEY_LOCAL_MACHINE
HKCU will expand to HKEY_CURRENT_USER
HKU will expand to HKEY_USERS
etc.

nycgags
Posts: 3
Joined: 05 Jan 2011 16:07

Re: having problems using parameters in a batch file

#5 Post by nycgags » 07 Jan 2011 09:35

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...

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: having problems using parameters in a batch file

#6 Post by aGerman » 07 Jan 2011 14:46

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

Post Reply