Search code examples
powershellvmwarepowercli

Comparing 2 objects in PowerCLI


I apologies if the question is not quite right, as I'm not sure of the correct wording / syntax for this question...

Get-View -ViewType VirtualMachine | Where { $_.Guest.GuestFullname} | Sort Name |Select-Object Name, @{N=”SelectedOS”;E={$_.Guest.GuestFullName}}, @{N=”InstalledOS”;E={$_.Summary.Config.GuestFullName}} | Out-GridView

How would I compare and match the data of "SelectedOS" and "InstalledOS" to be output.

So for example the current script would output:

Name                                                          SelectedOS                                                    InstalledOS
----                                                          ----------                                                    -----------
VM-Demo-CCMIVR-1                                         Microsoft Windows Server 2012 (64-bit)                        Microsoft Windows Server 2012 (64-bit)
VM-Demo-vMCD2                                            Other 2.6.x Linux (32-bit)                                    CentOS 4/5/6 (32-bit)
VM-Inf-CUC-10-5                                          Red Hat Enterprise Linux 6 (64-bit)                           Red Hat Enterprise Linux 6 (64-bit)
VM-Inf-CUCM-10-5                                         Red Hat Enterprise Linux 6 (64-bit)                           Red Hat Enterprise Linux 6 (64-bit)
VM-Inf-DC01                                              Microsoft Windows Server 2012 (64-bit)                        Microsoft Windows Server 2012 (64-bit)

However i only want to see:

Name                                                          SelectedOS                                                    InstalledOS
----                                                          ----------                                                    -----------
VM-Demo-CCMIVR-1                                         Microsoft Windows Server 2012 (64-bit)                        Microsoft Windows Server 2012 (64-bit)
VM-Inf-CUC-10-5                                          Red Hat Enterprise Linux 6 (64-bit)                           Red Hat Enterprise Linux 6 (64-bit)
VM-Inf-CUCM-10-5                                         Red Hat Enterprise Linux 6 (64-bit)                           Red Hat Enterprise Linux 6 (64-bit)
VM-Inf-DC01                                              Microsoft Windows Server 2012 (64-bit)                        Microsoft Windows Server 2012 (64-bit)

Solution

  • If I read you right, you want to compare and only show machines where "SelectedOS" and "InstalledOS" are the same. To do that you need the -eq in your Where statement. Like this:

    Where { $_.Guest.GuestFullname -eq $_.Summary.Config.GuestFullName }
    

    So your code becomes.

    Get-View -ViewType VirtualMachine | Where { $_.Guest.GuestFullname -eq $_.Summary.Config.GuestFullName } | Sort Name |Select-Object Name, @{N=”SelectedOS”;E={$_.Guest.GuestFullName}}, @{N=”InstalledOS”;E={$_.Summary.Config.GuestFullName}} | Out-GridView