Search code examples
xmlpowershellcomparecompareobject

Comparing Object, include Computername in output but do not use it


I want powershell to Compare a few XML files that contain services, status and computernames. That include all of these in the output but only use Name and Status for comparison. The Idea is to get an output like so:

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

As you can see the table looks nice but it doesn't include the computernames... Anybody got an idea to help out? This is what the code looks like.

$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

Solution

  • You can add -Passthru to Compare-Object to get the extra fields:

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

    Example:

    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
    

    I'm just showing 1 to save space.