Set-NetworkAdapter The operation for the entity VirtualMachine-vm- failed with the following message: “Invalid configuration for device ‘0’.”

When trying to set the connected property of a network card within virtual machine you receive:

Get-VM -name XYZ | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$true 
Set-NetworkAdapter : 7/6/2012 10:59:14 AM    Set-NetworkAdapter        The operation for the entity VirtualMachine-vm-777 failed with the following message: "
Invalid configuration for device '0'."

If you use vCenter Server 5.0, vCenter Server 5.0 Update 1, ESXi 5.0, and ESXi 5.0 Update 1. then you could be probably affected by this problem described here
What will happen :
You will notice that your vm nics are not “connected” although they have setting “connect at power on” checked.
If you will try to check the “connected” property you will receive error “Invalid configuration for device ‘0’
There is no way to fix this issue on a permanent basis. All you can do now is to proceed with vmware KB 2013639 in order to fix your vms. There is a powercli script attached in the KB, download it and execute it:

# Get-VM | Test-VDSVMIssue
This will show you which vm is affected
# Get-vm | Test-VDSVMIssue -fix  
This will try to fix issue for all your vms 

If you want to test it first for a particular vm select one using :

get-vm -name "your vm" | Test-VDSVMIssue -fix

What happens is that if your vm is affected by this issue, it looses information about its vmnic portid in the vmnic portgroup.
The script provided by vmware fixes this by changing the portid for this vmnic and then reverting the portid to the old portid.
You can check it manually if you are affected by doing following steps:
Open vSphere client , switch to networking view, and select the portgroup where your vm has vmnics attached.
Open your vm vmx file to check which portid was configured. You can read the vmx file using ssh to esxi host or download the vmx file using datastore browser within vSphere client.
When your vm vmnic is using distributed vswitch you will see entries like those:

ethernet0.dvs.switchId = "8d 7e 0b 50 67 e4 0b 46-3f 30 0b f8 d5 6a 58 f3"
ethernet0.dvs.portId = "1234"

In case you are affected, you will notice that although you can see that this vmnic is using this portid(verified by checking portgroup in vSphere client) there is no file on datastore
in vds directory ‘.dvsData/ethernet0.dvs.switchId’ where ethernet0.dvs.switchId should be changed to your switch id
That’s why we can not change the state of vmnic to connected.
Run the vmware powercli script from KB, it should tell you that problem was fixed.
If you will now check the .dvsData/switchID directory you should find a file with name of the portID that was choosen for this vmnic
From that moment go to vm and select “connected” property to on. You should not see the Invalid configuration for device ‘0’ error again.
You can experience this problem even if your vms are not using sdrs.
If you have large amount of vms that are affected by this and you want to automate process of selecting “connected”, you can automate checking the ‘connect’ box property using :

get-vm -Name AffectedVM | Get-NetworkAdapter  | select connectionstate 
#check if it is disconnected, after the applying the fix for this vm use
get-vm -Name AffectedVM | Get-NetworkAdapter  | Set-NetworkAdapter -Connected:$true -Confirm:$false
#this will enable the 'connected' property

if you have more than 1 vm for which you want to edit this property you can list them in this one command

get-vm -Name AffectedVM,AffectedVM2,AffectedVM3 | Get-NetworkAdapter  | Set-NetworkAdapter -Connected:$true -Confirm:$false 

Check vm vmnic vlan

I wanted to get a list of vms that are using specific vlan so i wrote this one:

foreach($vm in (get-vm)){
if (Get-NetworkAdapter -vm $ |?{$_.NetworkName -eq "network23" -or $_.NetworkName -eq "network65"}){$}

Ok, that was working but it was VERY VERY VERY VERY VERY slow…

So i wrote it using get-view :

$vms=get-view -viewtype virtualmachine
foreach($vmview in $vms )
$ntwkview=get-view -id $
foreach($ntwkentry in $ntwkview){
if ($ -eq "network23" -or $ -eq "network65")

This one needed only 3 minutes to complete on a ~1000vm infrastructure. I stop the first script after 30minutes of running as it would probably finish next day …

The script is not perfect… yet 😉 but it will do the job and what’s most important quickly. I hope this will save you some time on reporting.

vmnic renumeration

I had an issue with esx 3.5u2 box, that had wrong vmnic numeration.
In order to correct it edit file /etc/vmware/esx.conf
find entries that describes your vmnics /device/000:xxx:xxx… = “vmnicX”
Rewrite it to normal.
If you had quad port ethernet card and you want to have numbers 4 nics to vmnic 2,3,4,5 and 4 nics to 6,7,8,9 then read the descriction below in that file, where vmnics are described with 3 lines per each. You will see mac addresses for each nic. You will be able to tell that 4 nics have the same xx:xx:xx:xx:xx:yy Xs. So there should be 4 nics that have the same x groups. 40bits of the mac address were the same in my situation, only last 8 bits were different.
After that delete all information for vmnics
3 entries per 1 vmni

Sometimes there can be additional lines. Just remember the number of child[xxxx] which you are deleting.
Reboot esx. It’s all done.
I also saw one document that was describing this situation when this did not help. Maybe it will be useful for somebody: