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?
Getting acquainted with PowerShell
PowerShell (POSH) is an area in which I have not explored yet. Colin Smith (blog | twitter) and Zach Mattson (blog | twitter) both presented topics about POSH and unfortunately I could not attend either, but that’s a different story. Today I felt I needed to take a break from my certification studies and start getting familiar with Microsoft’s new scripting language.
We have a POSH user group here in Arizona which is appropriately called the Arizona PowerShell Users Group. Though I haven’t attended a meeting yet the subjects that are covered is nonetheless intriguing. Jason Helmick (blog | twitter) and Mike Pfeiffer (blog | twitter) are the officers of the user group and their blogs contain a lot of information on how to get started.
Using Jason’s example from his video post: QuickBit: Executing scripts in PowerShell I saw him execute a cool script that utilized the system speech and it literally spoke what he had written. I used the script editor and ran the following:

script syntax
[void][System.Reflection.Assembly]::LoadWithPartialName("System.speech")
$Speak=new-object System.Speech.Synthesis.SpeechSynthesizer
$Speak.speak("SQL Saturday 47 Rocks!")
Another thing that Jason pointed out that I saw as really handy was the fact you can output the results to a grid view. Yeah a grid view!


script syntax
Get-Service | Out-GridView
The cool thing about output to grid is the fact that you have filtering capabilities and sorting. That’s a big win in my eyes!
results

I really dig command-line administration and I should have dove right into this but I didn’t. Now I need to begin my search for some good books and reference sites which Jason also refers to his video post: DrillBit: PowerShell books I keep. Hopefully I will have some scripts to post and come up with some ideas to help automate things. I’ll probably start with converting some of my old batch and VBS script first and go from there.
Create Virtual Directory in IIS 6.0 via Command line
Well for most of my implementations I deploy variations of Windows 2003 Server with x86 and x64 editions included. I always use IIS because I deploy web based applications so I like to keep things standardized as much as humanly as possible. Its just better that way. No arguments necessary.
Well in order to keep settings consistent across the board I need to ensure all of my IIS settings are intact. My implementations utilize virtual directories, but I also like to have a separate application pool associated to this virtual directory for obvious reasons. In the event you are not familiar: separating applications into their own application pool or group is called (isolation mode). This allows a site or a group of sites to be taken offline without affecting all sites that might reside on that server. If you have a application server that hosts many sites hopefully you have them configured using their own application pools.
In terms of default documents I like to specify only what I need which is typically (default.aspx) while removing everything else. Then I set the framework version to .NET 2.0. Of course I prefer to do this auto-magically to remove chances of human error so no manual labor for me I tell ya!
Below is what the contents of my batch file looks like. Feel free to use it if you find it helpful. Of course you want to test it on a test or dev environment first. It has worked for me several times but environments differ and you never know. Besides this particular script works if you have the inetpub on the C:\. (Notice Line 7)
Also note that this script is dependent on two VBS scripts (adsutil.vbs & IISvdir.vbs) both of which you will find on the Windows Server platforms and not on the desktops.
@echo off color 17 Title My Standard IIS 6.0 Configuration . . . :: **************************************** SET var=MyApplicationName SET dir=C:\Inetpub\AdminScripts SET ito=120 SET dd=Default.aspx :: **************************************** :: ======================================== :: STEP 1: CREATE APPLICATION POOL :: ======================================== echo Creating Application Pool . . . echo ------------------------------------------------------------ CSCRIPT //nologo %dir%\adsutil.vbs CREATE w3svc/AppPools/%var% IISApplicationPool echo. :: ======================================== :: STEP 2: SET IDLE TIMEOUT :: ======================================== echo Setting Idle Timeout . . . echo ------------------------------------------------------------ CSCRIPT //nologo %dir%\adsutil.vbs SET w3svc/AppPools/%var%/IdleTimeout %ito% echo. :: ======================================== :: STEP 3: CREATE VIRTUAL DIRECTORY :: ======================================== echo Creating Virtual Directory . . . echo ------------------------------------------------------------ CSCRIPT //nologo %systemroot%\system32\IISvdir.vbs /create "Default Web Site" "%var%" %systemdrive%\inetpub\wwwroot\%var% echo. :: ======================================== :: STEP 4: SET .NET VERSION to 2.0 :: ======================================== echo Setting .NET Version . . . echo ------------------------------------------------------------ %windir%\Microsoft.Net\Framework\v2.0.50727\aspnet_regiis.exe -s w3svc/1/root/%var% echo. :: ======================================== :: STEP 5: SET DEFAULT DOCUMENT :: ======================================== echo Setting Default Documents . . . echo ------------------------------------------------------------ CSCRIPT //nologo %dir%\adsutil.vbs SET w3svc/1/Root/%var%/DefaultDoc %dd% echo. :: ======================================== :: STEP 6: ASSIGN APPLICATION TO VIRDIR :: ======================================== echo Assigning Application Pool to Virtual Directory . . . echo ------------------------------------------------------------ CSCRIPT //nologo %dir%\adsutil.vbs SET w3svc/1/Root/%var%/AppPoolid %var% echo. PAUSE
Before we start running scripts let’s take a look at how IIS currently looks like. This way we can have a before and after comparison. So below is a screen cast of my demo system’s IIS. As you can see it is very clean because it’s a fresh install. I haven’t really done anything to it yet. Once we run the script we will see a newly created application pool and a newly created virtual directory.

Below is what the script looks like once it is executed on a server system that has IIS installed.

Now let’s verify the differences between the before and after. As you can see the Application Pool & Virtual Directory have been created…

Looking into the details
The default Idle Timeout is now 120 mins. In a typical install this is defaulted to 20 mins.

Virtual Directory details… this is pretty much the same but in this case it was configured via script.

Again… in a typical setup you would usually see (Default.htm, Default.asp, index.htm and iisstart.htm) listed as default documents. In this case we only specify what we really need and that is the (Default.aspx).

And lastly the Microsoft.NET Framework is set to v2.0 rather than v1.1…

As you can see this is relatively straightforward and helps me keep implementations consistent.





