Change vnic type from / to e1000, Flexible, Vmxnet, EnhancedVmxnet, and Vmxnet3, and Unknown with Set-NetworkAdapter. UPDATED!

What if you noticed that your vms are running on wrong network card type ? Let’s say you want e1000 instead of vmxnet3 for some reason.There is a great cmdlet set-networkadapter. This will help us out in that kind of situation(Thx Rafael!).
Right, so what set-networkadapter can do for us ? (by this time you should read the get-help set-networkadapter -full). So now, like you already know 😉 it can do a lot for us! But what i want from it right now, is to help me with changing the adapter type. I want to change it from vmxnet 3 to e1000, would be cool if i will not need to shutdown vm too 😉 Unfortunately it will not be so cool at all !!! VM has to be powered off to make this change properly. Please do not do this while vm is up and running.
Continue reading

Report for all vms regarding uuid,network card, macaddress, ipaddress, network card type e1000/vmxnet in a csv ;)

Right,
i wanted to see all vms, with their current ips, macaddresses, names, networkcard types.
This report should also have all network cards in separate columns in csv. So i needed to dynamically adjust number of those ‘columns’.
Below script will output only ipv4 addresses without ipv6.

$reportedvms=New-Object System.Collections.ArrayList
$vms=get-view -viewtype virtualmachine  |Sort-Object -Property {  $_.Config.Hardware.Device |  where {$_ -is [VMware.Vim.VirtualEthernetCard]} |  Measure-Object | select -ExpandProperty Count} -Descending 

foreach($vm in $vms){
  $reportedvm = New-Object PSObject
  Add-Member -Inputobject $reportedvm -MemberType noteProperty -name Guest -value $vm.Name
  Add-Member -InputObject $reportedvm -MemberType noteProperty -name UUID -value $($vm.Config.Uuid)
$networkcards=$vm.guest.net | ?{$_.DeviceConfigId -ne -1}
$i=0
foreach($ntwkcard in $networkcards){
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $ntwkcard.Network
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $ntwkcard.Macaddress  
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $($ntwkcard.IpAddress|?{$_ -like "*.*"})
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.DeviceConfigId)}).gettype().name)
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}

$reportedvms|Export-Csv c:\myreport.csv

Ok, and what if somebody doesn’t want those multiple columns, but wants to have only 1 column that will describe all network cards for particular vm ?

$vms=get-view -viewtype virtualmachine
$(
foreach($vm in $vms){
$networkcards=$vm.guest.net | ?{$_.DeviceConfigId -ne -1}
""|select  @{n="VM name";e={$vm.name}},@{n="uuid";e={$vm.config.uuid}},@{n="net  info";e={[string]::join(',',  $($networkcards|%{$devid=$_.DeviceConfigId;[string]::join(',',$(($vm.config.hardware.device|?{$_.key  -eq $devid}).gettype().name,$_.network,($_.ipaddress -join ';'),$_.Macaddress))})  )}}
}
)|export-csv c:\myreport.csv

I know 😉 a lot of joining.
@Update: Luc has noticed my error there that i was not sorting $vms array. We have to sort it by the vm with most network cards so that export-csv will work properly.
@Update: Thanks Steve for the feedback. I have updated the code in both examples.
27/02/2014@Update : Fixed issue with reporting multiple ip addresses in second example.