VMware Cloud Community
Grzesiekk
Expert
Expert
Jump to solution

get-view -Filter issue

Hello,

  i wanted to ask if i am doing something wrong here , or the documentation is wrong for this Filter.

http://pubs.vmware.com/vsphere-50/index.jsp?topic=%2Fcom.vmware.powercli.ug.doc_50%2FGUID-CE297107-0...

in guide it says:

"

1

Create a filter by the power state and the guest operating system name of the virtual machines.

$filter = @{"Runtime.PowerState" ="poweredOn"; "Config.GuestFullName" = "Windows XP"}
2

Get a list of the virtual machines by using the created filter and call the ShutdownGuest method for each virtual machine in the list.

Get-View -ViewType "VirtualMachine" -Filter $filter | foreach{$_.ShutdownGuest()}

The filter gets a list of the powered-on virtual machines whose guest OS names contain the string  Windows XP. The Get-View cmdlet then initiates shutdown for each guest operating system in the list.

"
I wrote myself similar filter but i would like to see vms which are windowsfamily AND that are not templates so:
$filter=@{'Guest.GuestFamily'='windowsGuest';'Config.Template'='False'}
get-view -ViewType virtualmachine -Filter $filter |select name, @{n='y';e={$_.Config.Template}},@{n='yyy';e={$_.Guest.GuestFamily}}
as per help for get-view
"
-Filter <Hashtable>
    Specify a hash of <name>-<value> pairs, where <name> represents the property value to test, and <value> represents a pattern the property must match. If more than one pair is present, all the patterns must match.
"
so i have provided 2 pairs, one for config.template and one for guest.guestfamily. BOTH of them should be true in order to get the result as per documentation , but when i will run this i will receive a result of OR and not AND.
I will receive entires:
col1  col2    col3
vm1  False
vm2 False WindowsGuest
So why am i receiving entries with blan guestFamily, for me it looks like it's doing ' if i find 1 propert or 2nd property' and not '1 proeprty AND 2nd property' .
Can somebody confirm that this is how it works with and OR for pairs, or if i am thinking not properly here Smiley Wink
Thank you in advance!
--- @blog https://grzegorzkulikowski.info
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The Get-View Filter parameter works as described, but I'm pretty sure you hit a special situation.

I suspect you have some VMs that do not have the VMware Tools running.

A condition in the Filter where property is $null will always succeed it seems.

If you change the Filter like this, you should see the correct VMs.

$filter=@{"Guest.guestState"="^Running";"Guest.GuestFamily"="windowsGuest";"Config.Template"=[string]$false}
Get-View -ViewType "VirtualMachine" -Filter $filter | 
Sort-Object Name |
Select
Name,
  @{N="Guest State";E={$_.Guest.GuestState}},
 
@{N="Family";E={$_.Guest.GuestFamily}},
  @{N="Template";E={$_.Config.Template}}

The extra condition will make sure that Guest.GuestFamily is present and has a value.

Note, I don't think this behaviour of the Filter is what I think should be done. If the filter encounters a $null in one of the properties it should return $false for that condition, not $true.

There is room for improvement on this behaviour of the Filter


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

The Get-View Filter parameter works as described, but I'm pretty sure you hit a special situation.

I suspect you have some VMs that do not have the VMware Tools running.

A condition in the Filter where property is $null will always succeed it seems.

If you change the Filter like this, you should see the correct VMs.

$filter=@{"Guest.guestState"="^Running";"Guest.GuestFamily"="windowsGuest";"Config.Template"=[string]$false}
Get-View -ViewType "VirtualMachine" -Filter $filter | 
Sort-Object Name |
Select
Name,
  @{N="Guest State";E={$_.Guest.GuestState}},
 
@{N="Family";E={$_.Guest.GuestFamily}},
  @{N="Template";E={$_.Config.Template}}

The extra condition will make sure that Guest.GuestFamily is present and has a value.

Note, I don't think this behaviour of the Filter is what I think should be done. If the filter encounters a $null in one of the properties it should return $false for that condition, not $true.

There is room for improvement on this behaviour of the Filter


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

0 Kudos
Grzesiekk
Expert
Expert
Jump to solution

Hi Luc,

  i can now confirm that it works like you have written. Although it was nice try to get only those which are running, it will still output a vm which is running and the Family can be blank. I receive a list now all running vms and one of them had the Family column blank.

Now two interesting things:

1)

If you do filter like this :

$filter=@{"Guest.GuestFamily"="windowsGuest"}

Then i saw that by default it is not returning templates, all received vms have Template column set to 'False' (is this 100% default behaviour of get-view for virtual machine , that it will ommit the templates?)

2)

Why do i have to have vm running in order to check if this is a windows guest ? while having filter

$filter=@{"Guest.GuestFamily"="windowsGuest"}

applied, i see in the output vms with:

Name        Guest State      Family      Template

vm1         notRunning     windowsGuest     False

So for some reason it recognizes the familyOS for vms that are powered off.

Why do i have to have vmware tools instaled within guest os in order to get the vm family os ?

->

I have noticed that after creating a blank vm and selecting an os for it :

"Microsoft Windows 8 (32-bit)"

This is not populating the guest object

"

ToolsStatus                     : toolsNotInstalled
ToolsVersionStatus              : guestToolsNotInstalled
ToolsVersionStatus2             : guestToolsNotInstalled
ToolsRunningStatus              : guestToolsNotRunning
ToolsVersion                    :
GuestId                         :
GuestFamily                     :
GuestFullName                   :

"

But still i am able to check what OS is there by getting the

config.GuestFullName

this is still populated even if tools are not installed. And i think those fields are not populated the the vm is poweredOfff. So it is wrong to rely on the filed Guest.GuestFamily if we still want to receive vms that are poweredoff.

So this

$filter=@{"Config.GuestFullName"="windows";"Config.Template"=[string]$false}

Get-View -ViewType "VirtualMachine" -Filter $filter |
Sort-Object Name |
Select Name,
  @{N="Family";E={$_.Guest.GuestFamily}},
  @{N="Template";E={$_.Config.Template}},
  @{N="OSname";E={$_.Config.GuestFullName}}

Returns what i wanted, all vms which have windows in the name of the OS(this option need to be specified while creating vm though)

Again i think i finally understood now how this works. Thank Luc !

@update 😕

having filter like this

$filter=@{"Guest.GuestFamily"="windowsGuest"} from what you have said , should show me Empty ones and with windowsGuest, because empty should be counted as true.  But in fact i am receiving list with all Family: windowsGuest, no empty one. So it acts like it searchs vms only with vmware tools installed. Can you comment on this ?

Message was edited by: Grzesiekk

--- @blog https://grzegorzkulikowski.info
0 Kudos
LucD
Leadership
Leadership
Jump to solution

1) No, the Get-View -ViewType VirtualMachine also returns templates

2) When a VM is powered off but had VMware Tools running, the information collected by the VMware Tools is cached for a period of time.

So the GuestFamily will be there, but only for a limited time.

I have no clue how long that information is cached.

There is a difference between the Config.GuestFullName and Guest.GuestFamily.

The Config entry is what you specified when you created the VM.

The Guest entry is information collected by the VMware Tools.

The problem with the Config entry is that you easily specify Windows 7 for example, but then install a Linux distribution in the VM.

The Config entry will keep saying it is a Windows guest, while the Guest entry will contain the name of the Linux distribution.

The Config entry is primarily used to assign the correct devices and settings for the VM (note for example how the default size of Hard Disk 1 changes depending on the type of OS you select when creating a VM).


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

0 Kudos