I have been doing a lot of work with windows scripts (cmd/bat files, windows shell commands, vb scripts, etc) and wanted to share some of the things I have learned. I will be referring to bat files from here on out but you could also give your files the .cmd extension. Use whatever your comfortable with as there really is no difference on Windows XP and above.
How to load IP Addresses into your hosts file through script
The first thing your going to need if your going to work with bat files is a good editor. I haven’t found what I would consider to be an IDE for bat files (if you have any recommendations leave them in the comments) but the best text editor that I have found for bat files is NotePad++. I use the portable version and it at least provides syntax color coding for some of the basic elements of your bat files.
Now you can copy the following into a new text file and save it as ModifyHosts.bat
:: Misc Variables
SET IIS-Server_IP=10.200.5.215
SET SQL-Server_IP=127.0.0.1echo.
echo Load hosts file entries
echo %IIS-Server_IP% IIS-Server>> %SYSTEMROOT%\SYSTEM32\drivers\etc\hosts
echo %SQL-Server_IP% SQL-Server>> %SYSTEMROOT%\SYSTEM32\drivers\etc\hosts
The basic idea is that you create variables that store the IP address for each server your going to need to reference in your configuration files for your applications. Some common examples would be:
- web.config for an IIS web application
- Application bindings for BizTalk
- Connection strings in an application.config for a .Net application
- Etc
I follow the convention of <ServerName>_IP for these variables and set them before executing a build script against a newly provisioned server.
What this allows you to do is to abstract which actual server is being referred to in the configurations of your various applications. We have used this to maintain a single source controlled web.config for a production website that is deployed to development, test, and staging environments without having to change the web.config. The only settings that have to change are the ip addresses for the associated server names listed in the hosts file and that is all done by this script.
This also allows us to source control our BizTalk application’s bindings and then be able to deploy the same set of bindings to development, test, and staging environments. All of the bindings refer to server names that are set in the hosts files of each of the individual BizTalk servers.
Hope you find this useful. I will add additional tips as I have time. Thanks