比较PowerCLI中的2个对象

时间:2015-08-05 15:19:47

标签: powershell vmware 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)

1 个答案:

答案 0 :(得分:3)

如果我看对了你,你想要比较并且只显示那些" SelectedOS"和" InstalledOS"是相同的。为此,您需要-eq语句中的Where。像这样:

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