17 August 2007

Set IP setting for any Network Card from Command Line

Many administrators are familiar with an opportunity of cloning Windows OS (Sysprep). I already described process of cloning.

Recently, one my client has put a task, to install one Windows server, to set up all and to prepare system for cloning. Also it was required, that on all servers would be static IP addresses.

Equally, it's a not so complicated, but we have only one small problem. At cloning, on different servers Windows set different a name for network cards, once Local Area Connection, once Local Area Connection 2, sometimes Local Area Connection 5 and so on. As you understand, it makes a certain inconvenience at automation of process. To solve the given problem I’ve created a script, Set-IP.bat.

At start Set-IP.bat will ask what IP Address, Subnet Mask and Default Gateway to define on a network card of the given server. I wish to pay your attention that the script itself will determine a name of a network card and will rename on necessary to you, in my case I renamed it to Production Network 1. Before the Sysprep, I have created directory C:\Scripts and have put down there Set-IP.bat.

Then in key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

created a Key SET-IP in format REG_SZ with value C:\Scripts\SET-IP.bat

Here contents of SET-IP.bat:


@Echo off
SET /P IP=Please Enter the IP Address for this Server:
SET /P Subnet=Please Enter the Subnet Musk for this Server:
SET /P GW=Please Enter the IP Address of the Defaul Gateway for this Server:
SET /P DSRV=Please Enter the IP Address of the DNS for this Server:


For /F "tokens=3* delims= " %%a in ('ipconfig^|find /i "Ethernet adapter "') do Set NIC=:%%a %%b
For /F "tokens=1 delims=:" %%c in ('echo %NIC%^|find /i "Local Area"') do Set NIC=%%c

netsh.exe interface set interface name = "%NIC%" newname = "Production Network 1"
netsh interface ip set address "Production Network 1" static %IP% %Subnet%
netsh interface ip set address "Production Network 1" gateway = %GW% gwmetric = 1
netsh interface ip set dns "
Production Network 1" static %DSRV%

The End! :)


Bookmark and Share ITStuff.ca

No comments:

Post a Comment