无法从Compare-Object获得所需的结果

时间:2017-06-28 07:15:50

标签: powershell powershell-v4.0 powercli

示例代码:

$hosts    = Get-View -ViewType hostsystem -Property name,network
$clusters = Get-View -ViewType clustercomputeresource -Property name,network,host

$hosts | Add-Member -MemberType AliasProperty -Name TEST -value MoRef
$clusters | Add-Member -MemberType AliasProperty -Name TEST -Value Host

我想在解析数据时加快脚本速度,并希望使用Compare-Object

然而,代码

Compare-Object $hosts $clusters[0] -Property TEST -IncludeEqual -ExcludeDifferent -PassThru

没有输出。

代码

Compare-Object $hosts.test $clusters[0].test -IncludeEqual -ExcludeDifferent -Pass -PassThru

给我完全匹配,但没有通过对象。

不幸的是我无法确定原因。

1 个答案:

答案 0 :(得分:0)

在行$hosts | Add-Member -MemberType AliasProperty -Name TEST -value MoRef中,您尝试设置别名属性MoRef,但对于第一行$hosts = Get-View -ViewType hostsystem -Property name,network中的主机,您没有选择此属性,因此无法设置此别名。

请检查要比较的对象是否包含正确的信息。

Compare-Object $hosts.test $clusters[0].test -IncludeEqual -ExcludeDifferent -Pass -PassThru会出错,因为-Pass-PassThru都在这行代码中。 此行不会通过对象,因为您只是比较一个属性而不是整个对象。

Compare-Object $hosts $clusters[0] -Property TEST -IncludeEqual -ExcludeDifferent -PassThru看起来很好,我认为这应该在参数完成时起作用。