首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >格式化比较-对象输出

格式化比较-对象输出
EN

Stack Overflow用户
提问于 2021-02-12 16:19:11
回答 1查看 249关注 0票数 0

大家好,早上好。

我只是想看看是否可以将比较对象的输出格式化为pscustomobject。我看了以下类似的问题:

...but,我只是不太理解它的概念。我不想问那些已经被回答过的与此相关的问题,但是,我只是有点困惑。第三个环节是我开始理解的唯一一个环节,但仍然迷失在酱汁中。

只需要根据SideIndicator返回属性来分隔输出。

代码语言:javascript
复制
$Reference = Get-Content "C:\Users\Abe\Desktop\onj1.txt"
$Difference = Get-Content "C:\Users\Abe\Desktop\onj2.txt"


Compare-Object -ReferenceObject $Reference -DifferenceObject $Difference -IncludeEqual | Tee-Object -Variable CompareObject | Out-Null
$compareObject | Foreach { 
$ONJ1 = $_.SideIndicator -eq "<=" | Select $_.InputObject
$ONJ2 = $_.SideIndicator -eq "=>" | Select $_.InputObject
$Both = $_.SideIndicator -eq "==" | Select $_.inputobject

    [pscustomobject]@{
        ONJ1 = $ONJ1
        ONJ2 = $ONJ2
        Both = $Both
                }
        } 

不幸的是,所有3列都返回相同的输出,并假设im选择了InputObject属性,但是,它会将它们全部捕获。

编辑:正如Zett42所解释的

代码语言:javascript
复制
$Reference = Get-Content "C:\Users\Abe\Desktop\onj1.txt"
$Difference = Get-Content "C:\Users\Abe\Desktop\onj2.txt"


Compare-Object -ReferenceObject $Reference -DifferenceObject $Difference -IncludeEqual | Tee-Object -Variable CompareObject | Out-Null

$null = [array]$ONJ1 = $compareObject | Where-Object {$_.SideIndicator -eq "<="} | Select-Object -ExpandProperty InputObject
$null = [array]$ONJ2 = $compareObject | Where-Object {$_.SideIndicator -eq "=>"} | Select-Object -ExpandProperty InputObject
$null = [array]$Both = $compareObject | Where-Object {$_.SideIndicator -eq "=="} | Select-Object -ExpandProperty InputObject


For($i=0; $i -lt ($ONJ1.Count + $ONJ2.Count + $Both.Count); $i++){
    [pscustomobject]@{
        ONJ1 = $ONJ1[$i]
        ONJ2 = $ONJ2[$i]
        Both = $Both[$i]
                }
        } 
EN

回答 1

Stack Overflow用户

发布于 2021-02-12 16:39:40

不确定您所期望的格式化输出是什么,但可能是一些东西;请这样做?

onj1.txt

代码语言:javascript
复制
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor.

onj2.txt

代码语言:javascript
复制
Lorem ipsum dolor sit amet, 
sed do eiusmod tempor.
代码语言:javascript
复制
$Reference = Get-Content "C:\Users\Abe\Desktop\onj1.txt"
$Difference = Get-Content "C:\Users\Abe\Desktop\onj2.txt"

$compare = Compare-Object -ReferenceObject $Reference -DifferenceObject $Difference -IncludeEqual

[PsCustomObject]@{
    ONJ1 = ($compare | Where-Object {$_.SideIndicator -eq "<="}).InputObject -join [environment]::NewLine
    ONJ2 = ($compare | Where-Object {$_.SideIndicator -eq "=>"}).InputObject -join [environment]::NewLine
    Both = ($compare | Where-Object {$_.SideIndicator -eq "=="}).InputObject -join [environment]::NewLine
} | Format-Table -Wrap

输出:

代码语言:javascript
复制
ONJ1                                                      ONJ2                         Both                  
----                                                      ----                         ----                  
Lorem ipsum dolor sit amet,                               Lorem ipsum dolor sit amet,  sed do eiusmod tempor.
consectetur adipiscing elit,                                                                                 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66175341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档