VMware Cloud Community
DZ1
Hot Shot
Hot Shot
Jump to solution

Why can't they be the same?

I was checking for floppy drives on VMs and I came across something that I don't understand.  Before, I was using $VMname.floppydrives, and just saying if the count was greater than 0, then show me the VMs.  Well, since I received this: "WARNING: 'FloppyDrives' property is obsolete. Use 'Get-FloppyDrive' cmdlet instead."  I decided to use Get-VM | where { ($_ | Get-FloppyDrive).count -gt 0 }, but that does not work, I have to specify the array with Get-VM | where { @($_ | Get-FloppyDrive).count -gt 0 }  Now, if I run it for Network Adapters, like this Get-VM | where { ($_ | Get-NetworkAdapter).count -gt 0 } without the "@", that works. 

So why does one have to be specifed as an array for it to work, and the other one doesn't?  Everything is working just fine, but I would like to really increase my understanding. 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It is perhaps better to test like this

Get-VM | where { $_ | Get-FloppyDrive}

If there is no floppy drive on the VM the expression will evaluate to $null, and the VM will not be passed through the Where-clause. In all other cases (1 or more floppy drives) the VM will be passed.

The same should be true for the NICs

Could it be that the VM has more than 1 NIC ?

In that case you would have an array, and the Count property will exist.


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

It is perhaps better to test like this

Get-VM | where { $_ | Get-FloppyDrive}

If there is no floppy drive on the VM the expression will evaluate to $null, and the VM will not be passed through the Where-clause. In all other cases (1 or more floppy drives) the VM will be passed.

The same should be true for the NICs

Could it be that the VM has more than 1 NIC ?

In that case you would have an array, and the Count property will exist.


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

0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

Thanks for the reply, "Get-VM | where { $_ | Get-FloppyDrive}" works just fine, this was more of a "why does it work that way" question.  What's odd is if the VM has 1 NIC, it works, but it just does not work the same way for the floppy.  Just wondering, but it's no impact on work. 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange, for a VM with only 1 NIC I don't get an array back, but a single object.

What does this give for that specific VM ?

Get-VM | Get-NetworkAdapter | Get-Member


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

DZ1
Hot Shot
Hot Shot
Jump to solution

Wow, I royaly messed that up.  I ran the script before, but I would stop it after it pulled a few VMs, since I stated that I wanted something greater than zero, I thought it was pulling VMs if at last 1 or more.  You're right, if I say ($_ | Get-something).count -gt 0, it only pulls the data if it's greater than 1.  I tried Get-NetworkAdapter, and then I actually checked many VMs and they had at least 2 or more NICs, but now I'm more confused. 

I ran this "Get-VM where { ($_ | Get-NetworkAdapter).count -gt 0 } | Out-GridView", I only pulled VMs with at least 2 or more NICs

If I run "Get-VM | where { ($_ | Get-NetworkAdapter).count -gt 0 } | gm", or "Get-VM | where { ($_ | Get-NetworkAdapter) } | gm" I get  TypeName: VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl

But "Get-VM where { ($_ | Get-NetworkAdapter).count -gt 0 }" only gives me VMs with 2 or more NICs, even though it should be a VM with more than zero NICs, so 1 NIC or more. 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What you are seeing is correct imho.

When the VM has only 1 NIC, the Get-NetworkAdapter will return a single object, not an array of objects.

And the Count property is only available on arrays, not on single objects.

If you want to filter the VMs with 1 or more NICs, you could use the Where-clause I mentioned earlier.

Get-VM | Where {$_ | Get-NetworkAdapter} | Out-GridView

Or an alternative is to use the Measure-Object cmdlet with the Count property.

Get-VM | where {($_ | Get-NetworkAdapter | Measure-Object | Select -ExpandProperty Count) -ge 1} | Out-GridView


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