Last active
September 16, 2020 13:31
-
-
Save mccbryan3/a250c574c0b7b3937fb6b0d356b67711 to your computer and use it in GitHub Desktop.
Creates AD integrated DNS and DHCP records for vCenter based VMs. Uses runtime creds and assumes that DNS and DHCP are on the same server. Wild cards can be used for esx_vm_names.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[Parameter(Mandatory=$true)] | |
[string]$esx_vm_name, | |
[Parameter(Mandatory=$true)] | |
[string]$vcenter_build, | |
[Parameter(Mandatory=$true)] | |
[string]$scope_id, | |
[Parameter(Mandatory=$true)] | |
[string]$dhcp_server, | |
[Parameter(Mandatory=$true)] | |
[string]$zone_name, | |
$dry_run=$false | |
) | |
try { | |
Import-Module VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null | |
} catch { "Error providing modules "} | |
try{ | |
if(test-connection $vcenter_build -Count 1 -ErrorAction SilentlyContinue ) { | |
connect-viserver $vcenter_build | Out-Null | |
} | |
} catch { | |
"Erorr connecting to vcenter server";exit | |
} | |
Function configure_reservation { | |
param ( | |
[string] | |
$mac_address, | |
[string] | |
$reservation_name, | |
[string] | |
$scope_id, | |
[string] | |
$dhcp_server | |
) | |
try { | |
$dhcp_next_res = Get-DhcpServerv4FreeIPAddress -ScopeId $scope_id -ComputerName $dhcp_server | |
if(-not (Test-Connection $dhcp_next_res -Count 1 -ErrorAction SilentlyContinue)) { | |
Add-DhcpServerv4Reservation -ComputerName $dhcp_server ` | |
-IPAddress $dhcp_next_res -ClientId $mac_address ` | |
-Description "ESX Autodeploy - $reservation_name" ` | |
-ScopeId $scope_id -Name $reservation_name -WhatIf:$dry_run | |
} | |
} catch { | |
"Error setting IP reservation: $_"; exit | |
} | |
return $dhcp_next_res | |
} | |
function configure_dns_record { | |
param ( | |
[string] | |
$dns_server, | |
[string] | |
$record_name, | |
[string] | |
$record_ip, | |
[string] | |
$zone_name | |
) | |
try { | |
Add-DnsServerResourceRecordA -AllowUpdateAny:$true ` | |
-CreatePtr:$true -Name $record_name ` | |
-ComputerName $dns_server -ZoneName $zone_name ` | |
-IPv4Address $record_ip -WhatIf:$dry_run | |
$dns_record = (Get-DnsServerResourceRecord -ComputerName $dns_server -ZoneName $zone_name -Name $record_name).RecordData | |
} catch { | |
"Error setting DNS record: $_"; exit | |
} | |
return $dns_record | |
} | |
try { | |
$dhcp_resmacs = (Get-DhcpServerv4Reservation -ScopeId $scope_id -ComputerName $dhcp_server).ClientId | |
} catch { | |
"Erorr retrieving reservations from DHCP";exit | |
} | |
Get-VM -server $vcenter_build -Name $esx_vm_name | ForEach-Object{ | |
$vm_host = @{ | |
name = $_.name | |
mac = ($_ | Get-NetworkAdapter | Select-Object -First 1).macaddress.replace(":","-") | |
} | |
if($dhcp_resmacs -notcontains $vm_host.mac) { | |
$new_ip_addr = configure_reservation -mac_address $vm_host.mac.replace("-","") -reservation_name $vm_host.name -scope_id $scope_id -dhcp_server $dhcp_server | |
if($new_ip_addr) { | |
"Successfully Configured reservation for reservation_name $($vm_host.name) with mac $($vm_host.mac) scope_id $scope_id on $dhcp_server" | |
} | |
try { | |
$replicate_scope = Invoke-DhcpServerv4FailoverReplication -ComputerName $dhcp_server -ScopeId $scope_id -Force | |
} catch { | |
"Error Replicating DHCP Scrope $scope_id to partner"; exit | |
} | |
if($replicate_scope) { | |
"Successfully replicated $scope_id on $dhcp_server" | |
} | |
$new_dns_record = configure_dns_record -record_name $vm_host.name -record_ip $new_ip_addr -zone_name $zone_name -dns_server $dhcp_server | |
if($new_dns_record) { | |
"Successfully Configured dns record for reservation_name $($vm_host.name) with ip $new_ip_addr in zone $zone_name on $dhcp_server" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment