Thursday, November 9, 2017

creating l3 vmotion with powercli and csv

So sometimes the shortcomings of my vtool at 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.

#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 = $
    $args = $esxcliset.CreateArgs()
    $args.disabled = $false
    $args.netstack = 'vmotion'    
    # add vmkernel with netstack    
    $esxcliset = $ 
    $args = $esxcliset.CreateArgs()
    $args.interfacename = "$vmk"
    $args.netstack = 'vmotion'
    $args.mtu = "$mtu"    
    $args.portgroupname = "$pg"    
    # update networking to the vmkernel
    $esxcliset = $
    $args = $esxcliset.CreateArgs()
    $args.interfacename = "$vmk"
    $args.type = "static"
    $args.ipv4 = "$ip"
    $args.netmask = "$mask"
}#End of function