VMware Cloud Community
aaron416
Enthusiast
Enthusiast

PowerCLI sometimes fails to set NIC VLAN due to VM not being ready for config

We are in the middle of a migration project and I am seeing errors on some occasions when setting the network adapter VLAN. The error message, as reported by the PowerCLI exception is "Operation is not valid due to the current state of the object." I'd like to avoid these errors by determining if the VM currently has no active tasks (and thus nothing is preventing us from setting the network configuration).

So far, I've found one way to do it but I'm wondering if this is the best way. Here's my thought process:

do {

     $regVMView = get-view -viewType VirtualMachine -Filter @{"name"="vm-name-here"}

     sleep 10

} while( $regVMView.DisabledMethod -contains "ReconfigVM_Task" )

If the ReconfigVM_Task is disabled, we won't be able to map the network adapter to the new vSwitch. So therefore, wait for ReconfigVM_Task to be available and then proceed with setting the network adapter via set-networkadapter. Is this the best way to determine if the VM is ready for configuration or should I be checking somewhere else?

0 Kudos
5 Replies
LucD
Leadership
Leadership

As the 5.* API Reference states in the ManagedEntity object for the disabledMethod property, "releases after vSphere API 5.0, vSphere Servers might not generate property collector update notifications for this property"

To be absolutely sure you would have to use, as stated in that same text, the RetrievePropertiesEx or WaitForUpdatesEx methods of the PropertyCollector.

Let me know if you need some help in setting up this method call ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
aaron416
Enthusiast
Enthusiast

Hey LucD, an example method call in PowerCLI would be great. I did some research around the API but didn't see how to call it.

I should add that the migration script is running in Powershell, if that makes a difference.

0 Kudos
LucD
Leadership
Leadership

Hold on Heart


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
aaron416
Enthusiast
Enthusiast

Hey LucD, to clarify some things:

I'm fine with polling the status of the VM to see if it's ready to configure the NIC, which is what I expect that code snippet to do. I'm not sure what get-view does behind the scenes, but it seems like WaitForUpdatesEx is pretty deep into the API (and I'm not even sure if it's available for invocation in powershell).

-Aaron

0 Kudos
aaron416
Enthusiast
Enthusiast

Unfortunately, I haven't yet nailed down this error or what's causing it and the loop I posted above didn't work to fix it. I've also not been able to directly reproduce it in our lab.

If the VM is not ready for actions, the vCenter server is smart enough to queue up the reconfiguration task (invoked from set-networkadapter) and wait until it is ready to reconfigure. Powershell is unfortunately vague enough that it's telling me the object is not ready for configuration. Said object could be any of these:

1. the VM

2. nic0 on the VM

3. the VLAN via the VSM

Here's the code where it's actually set, and what I will probably break out into more lines:

get-networkadapter -vm $regVM -name $newvCenterInfo["nic$($i)"] | set-networkadapter -portgroup $portgroup -ErrorAction Continue -ErrorVariable nicSettingError

0 Kudos