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 specially for automated pools. We can set static IP address from DHCP server by creating reservation. We need some information for Virtual Machine which is MAC address, and of course we need information about DHCP server 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.
Next need to change format of MAC Address from 00:00:00:00:00:00 to 00-00-00-00-00-00, we can use Replace method.
Now let’s discover DHCP server on 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 command we need. Get-DhcpServerInDC and Get-DhcpServerv4Scope
Get-DhcpServerInDC will list all DHCP servers discovered in our Domain.
Get-DhcpServerv4Scope will list all scope we can use.
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 same command what before just change verb to Add.
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
-Name – Name of the VM – $mac.Parent
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.
Thank you for reading.