Hello,
i wanted to ask if i am doing something wrong here , or the documentation is wrong for this Filter.
in guide it says:
"
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.
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
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
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
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