After a long day of working with Microsoft’s IPAM feature, I found that it might be possible to deploy my virtual servers with a static IP Address without going to look up an IP from an Excel spreadsheet or some other log.
OK, let’s address the elephant in the room first. I know that there is this thing called DHCP and that I can already automatically assign an IP addresss, but with that solution, my IP Address could change from time to time. Typically, I create a DHCP Scope for servers that I’m just testing out, or need some dummy VMs with IP Addresses. This way I don’t have to worry about looking up stuff before deploying a VM that I’m going to destroy again shortly afterwards. I also use DHCP for PC’s, where I almost never care about the IP Address.
Microsoft’s IPAM feature, along with some handy powershell will keep my IP Addresses neatly organized, and I don’t have to hunt for new IP’s before deploying VMs.
First, you should deploy Microsoft IPAM on a Server 2012 R2 Server. (YES, this must be Server 2012 R2, or higher. Server 2012 is missing the required powershell commands, it’s not my fault). If you’re looking for setup instructions for IPAM check out this previous post.
Once your IPAM server is setup, make sure that you create an IP Address Range, like the one below.
You can see that I have one range that is 10.10.50.10-10.10.50.69 which is what I’ll be using for my static addresses. I also have a DHCP Scope of 10.10.50.70- 10.10.50.100.
Deploying a New Server
The new server has a few requirements as well. First, in order to connect to the IPAM server and get an IP Address, it’s going to have to have network connectivity. I got around this “chicken or the egg” scenario by having my server initially get a DHCP address, and then submit a request for a static IP. (Maybe this is hokey, but it worked).
Secondly, and possibly most disappointing is that the new servers need to have the IPAM tools installed on them. I found that without these tools installed, the powershell scripts won’t work because they’re missing the IPAM cmdlets. You can either install this in your virtual machine template, or install the features as part of the script we’ll use later.
Now we need the script to call. The script gets the next available free IP address, assigns it to the server, updates the IPAM server to record the entry, and then update DNS. My exact script is locted below. You will want to change the Server names, IP Addresses
##Check the following items: IPAM Server Name, Start and End IP Addresses of IPAM Range,
##Name of network adapter, Subnet Prefix Length, Default Gateway, DNS Server Addresses
##Requires Powershell 4.0 or higher
##Create a Common Information Model connection to IPAM Server
$cim = new-cimsession -ComputerName IPAM
##Find a free IP Address from the IPAM Server. Be sure to use the addresses in your range from the IPAM Server
$FreeIP = Get-IpamRange -StartIPAddress 10.10.50.10 -EndIPAddress 10.10.50.69 -CimSession $cim | Find-IpamFreeAddress | select-object -expandproperty IPAddress
##add the IP Address to the IPAM Server
$servername = hostname
Add-IpamAddress -CimSession $cim -IpAddress $FreeIP -devicename $servername
##Remove the CIM Session (logout)
Remove-CimSession -CimSession $cim
##Get the Network Adapter named "Ethernet"
$adapter = Get-NetAdapter -name Ethernet
$adapter | Set-NetIPinterface -dhcp disabled
##Set New IP AddressString
$adapter | New-NetIPAddress -addressfamily IPv4 -IPAddress $FreeIP -PrefixLength 24 -type Unicast -DefaultGateway 10.10.50.254
##Set DNS Server
set-dnsclientserveraddress -InterfaceAlias ethernet -ServerAddresses 10.10.50.12, 10.10.50.9
Create a guest customization that will call the script we created at first run. I've chosen to save the script on a file share so that if I decide to change it later, I only have to update one file.
When you deploy new VMs, IPAM will automatically get new records added so that you can keep track of them all in a single location. No need to update spreadsheets. No need to worry about IP Addresses changing due to a DHCP lease expiring.
I think there are plenty of other ways to do this, including automatically creating a DHCP Reservation, but it is a solution that might be used in part for other designs. Pieces of the script may be useful to cannibalize for Orchestrator or vCAC deployments to manage IP Addresses. Maybe you can use some or all of this yourself.