I spent way too much time staring at this today until it dawned on me that my VM might need to be running for certain associations to be activated and working.
All I was trying to do is to get a PowerShell script to return the MAC address of the Legacy Network Adapter of a VM.
Ben Armstrong has been more than generous at giving samples to the community and the updated associations sample (that should have given me exactly what I wanted) is here.
In his example he merrily selects his VM:
$vm = gwmi MSVM_ComputerSystem -filter "ElementName='Home Server'" -namespace "root\virtualization"
Then the very easily returns the vNIC:
$vnic = $vm.GetRelated("MSVM_EmulatedEthernetPort")
That is all fine and dandy for the example. But I tried it on a VM that is powered off, and you know what? I got nothing back. A NULL came back from WMI.
I searched for quite a long time trying to figure out what I was doing wrong.
In the end, I discovered I was doing nothing wrong. The VM has to be running for this association to return the vNIC.
I actually find this interesting. As once the object is created, it exists, and it has properties. I already powered the VM on and off for the MAC address to be automatically assigned. And then I can’t query it. It makes little sense to me. Maybe I can get Ben to respond and fill in that lack in my understanding.
In the end I simply modified my script. I have to briefly power on the VM and then off again to automatically assign the MAC, so why not take advantage of that.
Here is what I simply have now:
$vm = Get-WmiObject Msvm_ComputerSystem -Filter "ElementName=$vmName" -Namespace "root\virtualization"
$vm.RequestStateChange(2) # start
$vnic = $vm.GetRelated("Msvm_EmulatedEthernetPort") # this only returns while the VM is running
$vm.RequestStateChange(3) # stop