Quantcast
Channel: vStrong.info
Viewing all articles
Browse latest Browse all 149

Configure DHCP reservation with Powershell

$
0
0

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 = '10.80.24.0'
$IPaddress = '10.80.24.12'
$Server = 'SQL001'
$Domain = 'internal.vStrong.info'

$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.


Viewing all articles
Browse latest Browse all 149