Thursday, November 9, 2017

creating l3 vmotion with powercli and csv

So sometimes the shortcomings of my vtool at github.com/mrambig/vmware/vtool is that it assumes that the ip addresses are in range when creating vmkernel portgroups for vmware. It also assumes that your hosts are in order. We are working a huge VMware implementation which is being deployed and expanded in installment where this is not the case. This is something that i putting it here for those who might use this in future. Of course i have to still test this with my solution architects for whom I am doing this automation. I am certain that it will work. If not I will post the updated version as always in my github page. github.com/MrAmbiG/vmware


#start of function
function l3vmotion2csv
{
Write-Host "
A CSV file will be opened (open in excel/spreadsheet)
populate the values,
save & close the file,
Hit Enter to proceed
" -ForegroundColor Blue -BackgroundColor White
$csv = "$PSScriptRoot/HostVds.csv"
get-process | Select-Object vmhost,vss,portgroup,vlan,ip,mask,vmk,mtu | Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation
Start-Process $csv
Read-Host "Hit Enter/Return to proceed"

$csv = Import-Csv $csv

  foreach ($line in $csv) 
 {  # importing data from csv and go line by line
    $vmhost = $($line.vmhost)  
    $vss  = $($line.vss)  
    $portgroup  = $($line.portgroup)
    $vlan  = $($line.vlan)    
    $ip  = $($line.ip)
    $mask  = $($line.mask)
    $vmk  = $($line.vmk)    
    $mtu  = $($line.mtu)    
        
    $esxcli = get-vmhost $vmhost | get-esxcli -v2
    (get-vmhost $vmhost).name
    get-vmhost $vmhost | get-virtualswitch -Name $vss | New-VirtualPortGroup -Name $pg -VLanId $vlan -Confirm:$false
    # add vmotion netstack
    $esxcliset = $esxcli.network.ip.netstack.add
    $args = $esxcliset.CreateArgs()
    $args.disabled = $false
    $args.netstack = 'vmotion'    
    $esxcliset.Invoke($args)
    
    # add vmkernel with netstack    
    $esxcliset = $esxcli.network.ip.interface.add 
    $args = $esxcliset.CreateArgs()
    $args.interfacename = "$vmk"
    $args.netstack = 'vmotion'
    $args.mtu = "$mtu"    
    $args.portgroupname = "$pg"    
    $esxcliset.Invoke($args)   
     
    # update networking to the vmkernel
    $esxcliset = $esxcli.network.ip.interface.ipv4.set
    $args = $esxcliset.CreateArgs()
    $args.interfacename = "$vmk"
    $args.type = "static"
    $args.ipv4 = "$ip"
    $args.netmask = "$mask"
    $esxcliset.Invoke($args)  
    }
}#End of function

l3vmotion2csv