Skip to content

Instantly share code, notes, and snippets.

@olljanat
Last active March 20, 2019 10:56
Show Gist options
  • Save olljanat/c93263bd4be4a897a67fedb36c83f498 to your computer and use it in GitHub Desktop.
Save olljanat/c93263bd4be4a897a67fedb36c83f498 to your computer and use it in GitHub Desktop.
Nutanix AHV - Migrates specified virtual machines IP addesses to IPAM reservations to avoid two IPs to be reserved per server
<#
.SYNOPSIS
Migrates specified virtual machines IP addesses to IPAM reservations to avoid two IPs to be reserved per server
.DESCRIPTION
Finds servers which have two reserved IP addresses,
removes old NIC and add new NIC with second IP (static IP inside of VM).
Can be run online but causes small network hickup for servers.
.PARAMETER vmNames
List of virtual machine names to be prosessed.
.EXAMPLE
Connect-NutanixCluster -Server "prism" -UserName "admin" -AcceptInvalidSSLCerts
.\MigrateVmIPsToIPAM.ps1 -vmNames "server1","server2"
#>
param (
[string[]]$vmNames
)
If ((Get-NutanixCluster).IsConnected -ne $True) {
throw "Connect cluster first using Connect-NutanixCluster cmdlet"
}
ForEach ($Name in $vmNames) {
Write-Host "Processing server: $Name"
$VM = Get-NTNXVM | Where-Object {$_.vmName -eq $Name}
$VMip = $VM.ipAddresses[1]
$OldNic = Get-NTNXVMNIC -Vmid $VM.vmId
$NetworkUuid = $OldNic.networkUuid
$MacAddress = $OldNic.macAddress
If ($VMip -and $NetworkUuid -and $MacAddress) {
$Task = Remove-NTNXVMNIC -Vmid $VM.vmId -Nicid $MacAddress
while ((Get-NTNXTask -Taskid $Task.taskUuid).progressStatus -ne "Succeeded") {
Write-Host "Waiting that old NIC is removed from VM"
Start-Sleep -Seconds 10
}
$nicSpec = New-NTNXObject -Name VMNicSpecDTO
$nicSpec.macAddress = $MacAddress
$nicSpec.networkuuid = $OldNic.networkUuid
$nicSpec.requestedIpAddress = $VMip
$nicSpec.requestIp = $VMip
$Task = Add-NTNXVMNic -Vmid $VM.vmId -SpecList $nicSpec
while ((Get-NTNXTask -Taskid $Task.taskUuid).progressStatus -ne "Succeeded") {
Write-Host "Waiting that new NIC is added to VM"
Start-Sleep -Seconds 10
}
} Else {
Write-Warning "Cannot find server static IP, network uuid or NIC MAC address"
}
Remove-Variable VM
Remove-Variable VMip
Remove-Variable OldNic
Remove-Variable NetworkUuid
Remove-Variable MacAddress
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment