【问题标题】:Can't get desired results from Compare-Object无法从 Compare-Object 获得所需的结果
【发布时间】:2017-06-28 07:15:49
【问题描述】:

示例代码:

$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

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

很遗憾,我无法确定原因。

【问题讨论】:

  • 您的最后一个代码 sn-p 中有一个虚假的 -Pass。除此之外,这两个Compare-Object 语句对我来说都很好。能否提供样本数据(CSV 格式)?

标签: powershell powershell-4.0 powercli


【解决方案1】:

$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 看起来不错,我认为当参数完成时这应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-03
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    相关资源
    最近更新 更多