Configure DHCP reservation with Powershell

Quick Powershell script to add DHCP reservation for a VM on multiple DHCP servers:

Please refer to DHCP Server Cmdlets in Windows PowerShell for more details.

$DHCPservers = 'DHCP001', 'DHCP002'
$Scope = ''
$IPaddress = ''
$Server = 'SQL001'
$Domain = ''

$VM = Get-VM $Server

# $VM.NetworkAdapters.MacAddress returns MAC address in semicolon separated 00:50:56:00:00:00 format.
# Add-DhcpServerv4Reservation only accepts MAC address in either flat 005056000000 or dash separated 00-50-56-00-00-00 format.
$MACaddress = $VM.NetworkAdapters.MacAddress.Replace(":","")  

# If the Guest OS computer name is not available, the FQDN will be built based on the VM name and the domain name.
If ($VM.Guest.HostName -ne $null) {
$GuestName = $VM.Guest.HostName
else {$GuestName = $VM.Name+"."+$Domain}

foreach ($DHCPserver in $DHCPservers) {
Add-DhcpServerv4Reservation -ComputerName $DHCPserver -ScopeId $Scope -IPAddress $IPaddress -ClientId $MACaddress -Name $GuestName -Type DHCP -Confirm:$false

Hope this will help.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>




This site uses Akismet to reduce spam. Learn how your comment data is processed.