示例代码:
$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
给我完全匹配,但没有通过对象。
不幸的是我无法确定原因。
答案 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
看起来很好,我认为这应该在参数完成时起作用。