Archive
Get IP Address From Windows Command Line
I know many can and will say I can simply use ipconfig or ping the local computer name and to an extent that’s true. In my case I really only want the IP Address and nothing else, just the plain IP Address. I don’t want the extra verbiage that goes along with it.
To get started let’s run through a simple statement, but before we do know that this is geared towards a command prompt and not a batch. The syntax is slightly different.
Step 1: Get only one reply
ping %computername% -4 -n 1 | find /i "reply"

Step 2: Get all left of the colon
FOR /f "tokens=1 delims=:" %d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO ECHO %d

Step 3: Get the IP Address
FOR /f "tokens=1 delims=:" %d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %g IN ("%d") DO echo %g

Step 4: Get the first octet
You might question why you would only want the first octet and the answer is simple. Based on that single value I can determine what the backup share is. So if I were to return only the first octet into a stored procedure then it can dynamically perform backups accordingly to the appropriate share.
FOR /F "tokens=1 delims=:" %d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %g IN ("%d") DO FOR /F "tokens=1 delims=." %h IN ("%g") DO ECHO %h

At this point you might be asking yourself what the syntax means. Well here is the scoop using (Step 2) as a reference point. Well consider tokens as segments of a single item that is separated by a specific value.
Let’s examine the following string:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Looks pretty straightforward for the most part but if you think about what the separating value that you’ll want to use then the string begins to appear differently. For example I want to set the delims otherwise known as the deliminator character to a colon. Well there is only one colon therefore making two tokens. All characters left and all character right of the colon.

So by me running (Step 2) I am essentially requesting all characters to the left of the colon, because I am only asking for token 1. If I specified token 2 then I would get all characters to the right of the colon including the leading space.
FOR /f "tokens=2 delims=:" %d IN ('ping %computername% -4 -n 1 ^| find /i "reply"') DO ECHO %d

Now moving onto (Step 3) I am essentially breaking apart the string into three tokens because I am setting the delims to a space which is represented by delims= “. There is a space between the = and the “.
Let’s examine the string:
Reply from 127.0.0.1
Hopefully at this point you are able to see the three tokens in the above string. So in order to return only the IP Address I only request token 3.
POP QUIZ
Q1. What would be the delims value for 127.0.0.1?
Q2. How many tokens will be as a result?
Q3. What token will I need to request to get the first octet?
Installing SQL Server 2005 Express via Command Line
There are a few ways to install SQL Server 2005: 1) From the UI or 2) Command-Line. The UI installation is cool, but deploying via command-line is (in my opinion) better, faster and more importantly consistent depending if you have all parameters specified in a batch file. There are technically two command-line options: 1) Pure Command-Line or 2) Command-Line with answer file.
pure command line
@echo off color 17 Title SQL Server 2005 Express Unattended Install . . . setup.exe /qb INSTANCENAME=DEV ADDLOCAL=SQL_Engine,SQL_Data_Files,SQL_Replication SECURITYMODE=SQL SAPWD=StrongPasswordGoesHere DISABLENETWORKPROTOCOLS=0
command line with answer file
@echo off color 17 Title SQL Server 2005 Express Unattended Install . . . setup.exe /qb /settings "AnswerFile.ini"
answer file contents
[options] INSTANCENAME=DEV ADDLOCAL=SQL_Engine,SQL_Data_Files,SQL_Replication SECURITYMODE=SQL SAPWD=StrongPasswordGoesHere DISABLENETWORKPROTOCOLS=0
In both of the above examples notice that I am not specifying installation paths, data file paths, etc… So for any parameter that I did not supply a value for will automatically use their default values for each option not implicitly set. I only needed the basic necessities listed for my subscriber deployments.
For more information about additional parameters visit: How to: Install SQL Server 2005 from the Command Prompt





