在 PowerCLI 中比较 2 个对象

Comparing 2 objects in PowerCLI

如果问题不太正确,我深表歉意,因为我不确定这个问题的措辞/语法是否正确...

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

如何比较匹配"SelectedOS"和"InstalledOS"的数据输出

例如,当前脚本将输出:

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)

不过我只想看:

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)

如果我没看错,您想比较并只显示 "SelectedOS" 和 "InstalledOS" 相同的机器。为此,您需要在 Where 语句中添加 -eq。像这样:

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

所以你的代码变成了。

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