PowerShell and DHCP – setting up IP address reservation for VMware VDI

PowerShell and DHCP – setting up IP address reservation for VMware VDI

Sometimes we need to set up static IP Address for VDI pool, it cannot be done on VDI site especially for automated pools. We can set static IP address from DHCP server by creating a reservation. We need some information for Virtual Machine which is MAC address, and of course, we need information about DHCP servers like server name and scope name. I will use PowerShell and PowerCLI to perform this tasks.

First, let’s retrieve MAC address of VDI desktops, we can use Get-NetworkAdapter command.

VMMacAddress

Get-NetworkAdapter to select Computer name and Mac Address

Next, need to change the format of MAC Address from 00:00:00:00:00:00 to 00-00-00-00-00-00, we can use Replace method.

MACAddressDashReplacment

Replacing “:” to “-“

Now let’s discover DHCP server on the network, PowerShell module for DHCP Server will be handy here. Running Get-Command -Module DHCPServer -Verb Get will give us list of command to retrieve information about DHCP Server. Highlighted two commands we need. Get-DhcpServerInDC and Get-DhcpServerv4Scope

DHCPModuleGetCommands

DHCP Server PowerShell module commands

Get-DhcpServerInDC will list all DHCP servers discovered in our Domain.

DHCPServer

DHCP Server

Get-DhcpServerv4Scope will list all scope we can use.

DHCPScope

DHCP Scope

Now we have all necessary information to create DHCP reservation for VDI desktops. We can discover which command we can use to do this task by running the same command what before just change the verb to Add.

AddDHCPReservation.jpg

Add DHCP Reservation command

Add-DhcpServerv4Reservation we need to input:
-ComputerName – DHCP Server name – $DHCPSvr.DnsName
ScopeId – Scope name to add reservation – $DHCPScope.ScopeId
-IPAddress – IP Address assign to VM – 192.168.1.101
-ClientId – MAC Address of the VM – $mac2[0]
-Name – Name of the VM – $mac[0].Parent

AddingReservation

Adding reservation to DHCP server

Now we can check by running Get-DhcpServerv4Reservation that new IP address reservation has been created. As long as this VM will not be deleted or MAC Address not changed every time will receive same “static” IP Address.

GetDHCPReservation.jpg

IP Address reservation

Thank you for reading.

Advertisements