Automation

 View Only
  • 1.  Move-VM to multiple Network Adapters

    Posted Nov 24, 2021 10:53 AM

    Hi LucD

     

    I  have a script to move VM's from vDS to NSX-T. The script works perfect with a single Network Adapter

    In my environment I have VMs with single Network Adapter and multiple Network Adapter and I would like to modify this script to cater for multiple Network adapters as well please help

    Foreach ($VMdetail in $VMdetails) {

     

    $VM = Get-VM -Name $VMdetail.VMNAME -ErrorAction SilentlyContinue

    If ($VM) {
    Write-Host "VM ==> $VM is a alive on the source VC ==>$global:DefaultVIServer" -ForegroundColor Yellow
    }
    Else {
    Write-Host 'VM' $VMdetail.VMNAME' Cannot be found' -ForegroundColor Magenta
    continue
    }


    ##### Check if there is a Destination ESXi host on the specified cluster If found proceed or skip to next record

    $destination = Get-cluster $vmdetail.ClusterNSXT -ErrorAction SilentlyContinue |Get-VMHost -Name $VMdetail.ESXiNSXT

    if ($destination)
    {
    Write-Host "VM will be placed on ESXi host ==>" -ForegroundColor Yellow $destination
    }
    Else {
    Write-host "Destination ESXi host" $vmdetail.ClusterNSXT "is not Accessible" -ForegroundColor Magenta
    continue
    }


    ##### Check if the Network Adapter for the VM is found If found proceed or skip to next record


    $networkAdapter = Get-NetworkAdapter -vm $VM -ErrorAction SilentlyContinue

    If ($networkAdapter)
    {
    Write-Host "VM Network Adaptor info ==>$networkAdapter" -ForegroundColor Yellow
    }
    Else
    {
    Write-Host "Network Adpater cannot be attahced and migration will fail" -ForegroundColor Magenta
    continue
    }


    ##### Check if the destination NSXT Portgroup is available, also check if the destination
    ##### networkAdapteNSXT PortGroup is found, if unavailable skip to the next record


    $destinationNSXPortGroup = Get-VirtualNetwork -name $VMdetail.PortGroupNSXT |where {$_.networktype -eq "Opaque"} -ErrorAction SilentlyContinue

    If ($destinationNSXPortGroup)
    {
    Write-Host "VM will be moved to following PortGroup ==>$destinationNSXPortGroup " -ForegroundColor Yellow
    }
    Else
    {
    Write-Host "NSX-T Overlay Switch or PortGroup cannot be found " -ForegroundColor Magenta
    continue
    }


    #**************************** KICK OFF THE MIGRATION *****************************

    # Move VM
    Write-Host "NSX-T VM Onboarding in progress..........." -ForegroundColor Yellow

    # Change VM Portgroup from vDS to nVDS
    Write-Host "Changing Portgroup from Distributed Switch to NSX-T nVDS Segments in progress......." -ForegroundColor yellow

    Move-VM -vm $VM -Destination $destination -NetworkAdapter $networkAdapter -Network $destinationNSXPortGroup -ErrorAction SilentlyContinue

    }

     

     



  • 2.  RE: Move-VM to multiple Network Adapters

    Posted Nov 24, 2021 12:35 PM

    The NetworkAdapter and the Network parameters on the Move-VM cmdlet, both accept arrays.
    Just make sure the order of the network adapters and the target networks correspond.
    See the remark on the Network parameter.



  • 3.  RE: Move-VM to multiple Network Adapters

    Posted Nov 24, 2021 12:49 PM

    Hi LucD

     

    Added an array for the portgroups it works when i have a VM with two Network Adapters and two portgroups assigned on the CSV

    However if my VM has a single Network adapter it throws an error

    #### Error#####


    Get-VirtualNetwork : Cannot validate argument on parameter 'Name'. The argument
    is null or empty. Provide an argument that is not null or empty, and then try
    the command again.
    At line:116 char:55
    + ... rtGroups += Get-VirtualNetwork -name $VMdetail.PortGroupNSXT2 -ErrorA ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Get-VirtualNetwork], Parameter
    BindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
    ation.ViCore.Cmdlets.Commands.Networking.GetVirtualNetwork


     

    #### Modified code ######

    Foreach ($VMdetail in $VMdetails) {



    $VM = Get-VM -Name $VMdetail.VMNAME -ErrorAction SilentlyContinue

    If ($VM) {
    Write-Host "VM ==> $VM is a alive on the source VC ==>$global:DefaultVIServer" -ForegroundColor Yellow
    }
    Else {
    Write-Host 'VM' $VMdetail.VMNAME' Cannot be found' -ForegroundColor Magenta
    continue
    }


    ##### Check if there is a Destination ESXi host on the specified cluster If found proceed or skip to next record

    $destination = Get-cluster $vmdetail.ClusterNSXT -ErrorAction SilentlyContinue |Get-VMHost -Name $VMdetail.ESXiNSXT

    if ($destination)
    {
    Write-Host "VM will be placed on ESXi host ==>" -ForegroundColor Yellow $destination
    }
    Else {
    Write-host "Destination ESXi host" $vmdetail.ClusterNSXT "is not Accessible" -ForegroundColor Magenta
    continue
    }


    ##### Check if the Network Adapter for the VM is found If found proceed or skip to next record


    $networkAdapters = Get-NetworkAdapter -vm $VM -ErrorAction SilentlyContinue

    If ($networkAdapters)
    {
    Write-Host "VM Network Adaptor info ==>$networkAdapters" -ForegroundColor Yellow
    }
    Else
    {
    Write-Host "Network Adpater cannot be attahced and migration will fail" -ForegroundColor Magenta
    continue
    }


    ##### Check if the destination NSXT Portgroup is available, also check if the destination
    ##### networkAdapteNSXT PortGroup is found, if unavailable skip to the next record

    $destinationNSXPortGroups = @()
    $destinationNSXPortGroups += Get-VirtualNetwork -name $VMdetail.PortGroupNSXT1 -ErrorAction SilentlyContinue |where {$_.networktype -eq "Opaque"} -ErrorAction SilentlyContinue
    $destinationNSXPortGroups += Get-VirtualNetwork -name $VMdetail.PortGroupNSXT2 -ErrorAction SilentlyContinue |where {$_.networktype -eq "Opaque"} -ErrorAction SilentlyContinue


    If ($destinationNSXPortGroups)
    {
    Write-Host "VM will be moved to following PortGroup ==>$destinationNSXPortGroups " -ForegroundColor Yellow
    }
    Else
    {
    Write-Host "NSX-T Overlay Switch or PortGroup cannot be found " -ForegroundColor Magenta
    continue
    }


    #**************************** KICK OFF THE MIGRATION *****************************

    # Move VM
    # Change Network Adapter METHOD 2

    Write-Host "NSX-T VM Onboarding in progress..........." -ForegroundColor Yellow

    # Change VM Portgroup from vDS to nVDS
    Write-Host "Changing Portgroup from Distributed Switch to NSX-T nVDS Segments in progress......." -ForegroundColor yellow
    Move-VM -vm $VM -Destination $destination -NetworkAdapter $networkAdapter -Network $destinationNSXPortGroup -ErrorAction SilentlyContinue

    }



  • 4.  RE: Move-VM to multiple Network Adapters

    Posted Nov 24, 2021 01:10 PM

    That error seems to say there is nothing in $VMdetail.PortGroupNSXT2