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.

Revisions

  1. olljanat revised this gist Mar 20, 2019. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions MigrateVmIPsToIPAM.ps1
    Original file line number Diff line number Diff line change
    @@ -29,15 +29,17 @@ ForEach ($Name in $vmNames) {
    $VMip = $VM.ipAddresses[1]
    $OldNic = Get-NTNXVMNIC -Vmid $VM.vmId
    $NetworkUuid = $OldNic.networkUuid
    $MacAddress = $OldNic.macAddress

    If ($VMip -and $NetworkUuid) {
    $Task = Remove-NTNXVMNIC -Vmid $VM.vmId -Nicid $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
    @@ -48,6 +50,11 @@ ForEach ($Name in $vmNames) {
    Start-Sleep -Seconds 10
    }
    } Else {
    Write-Warning "Cannot find server static IP or network uuid"
    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
    }
  2. olljanat created this gist Mar 20, 2019.
    53 changes: 53 additions & 0 deletions MigrateVmIPsToIPAM.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <#
    .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

    If ($VMip -and $NetworkUuid) {
    $Task = Remove-NTNXVMNIC -Vmid $VM.vmId -Nicid $OldNic.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.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 or network uuid"
    }
    }