比较对象,在输出中包含Computername但不使用它

时间:2014-05-15 15:51:18

标签: xml powershell compare compareobject

我希望powershell能够比较一些包含服务,状态和计算机名的XML文件。这包括输出中的所有这些,但仅使用名称和状态进行比较。 想法是得到这样的输出:

Name Status SideIndicator PSComputername ---- ------ ------------- -------------- Appinfo Running <=
Appinfo Stopped =>
AudioEndpointBuilder Stopped =>
AudioEndpointBuilder Running <=

正如您所看到的,该表看起来不错,但它不包括计算机名... 有人有想法帮忙吗? 这就是代码的样子。

$WBService1 = Invoke-Command  $WB1 -Credential ($WBCred) -ScriptBlock {Get-      Service} |
    Select-Object Name, Status, DisplayName, PSComputerName |
    Sort-Object Name
 $WBService2 = Invoke-Command  $WB2 -Credential ($WBCred) -ScriptBlock {Get-Service} |
    Select-Object Name, Status, DisplayName, PSComputerName |
        Sort-Object Name
$WBService1 | Export-Clixml $FileOutput\WBService1.xml
$WBService2 | Export-Clixml $FileOutput\WBService2.xml
$Service1 = Import-Clixml $FileOutput\WBService1.xml
$Service2 = Import-Clixml $FileOutput\WBService2.xml
Compare-Object $Service1 $Service2 -Property Name, Status | Sort-Object Name | Format-     Table -AutoSize Name, Status, Sideindicator, PSComputername

1 个答案:

答案 0 :(得分:2)

您可以将-Passthru添加到Compare-Object以获取额外的字段:

Compare-Object $Service1 $Service2 -Property Name, Status -Passthru

示例:

PS> (compare $a $b -property name,status -PassThru |sort name)[0] |ft -a name,status,sideindicator,pscomputername

Name                      Status  SideIndicator PSComputerName
----                      ------  ------------- --------------
AdobeFlashPlayerUpdateSvc Stopped <=            adil-win8

我只是显示1以节省空间。