Monday, 8 April 2013

vCloud network bulk change

I have recently started work with an electronic payments company, in the vCloud team. Because of a network re-design, it was necessary to take 100 VMs (once they were powered off), and change the vApp network they were plugged into. There was also a requirement to give each VM a manually specified IP address.

This is how I did it;

A table of vm hostnames (column 1) and target IP addresses (column 2) was created in Excel and saved as a csv.
The below script then reads the file and changes the network to the one in that vapp called “desktop-org<something>”, and gives the VM  the IP address from column 2. The VM must be off.

$csv = import-csv h:\ps1\book1.csv

foreach ($line in $csv) {

 write-host **** Changing $line.vm to new network and $line.ipaddress ****

$targetvm = $line.vm

$targetip = $line.ipaddress

$targetvAppnet = get-civm $targetvm | foreach-object {get-civapp $_.vapp} | get-civappnetwork | where {$_.name -match "Desktops-org"}

get-civm $line.vm | Get-CINetworkAdapter | Set-CINetworkAdapter -IPAddressAllocationMode Manual -ipaddress $targetip -VAppNetwork $targetVAppNet -connected $true

}