在 PS 脚本中格式化 ForEach-Object 输出

Formatting ForEach-Object Output in PS Script

我正在尝试从我的 PowerShell 脚本中的 CSV 文件输出数据(RUSH 的歌曲和专辑列表),并且我按照我需要的方式对数据进行了排序,但我无法弄清楚如何按照我需要的方式格式化它。以下是我目前的代码是这样写的:

    $output = Import-CSV $DatabasePath -Delimiter "`t" | Sort-Object @{Expression= 'Year'; Descending = $true}, @{Expression='Song'; Ascending=$true}
    #$output
    $output | ForEach-Object {
        Write-Host "$_.Album "($_.Year)" "`n""
        Write-Host "$tab $_.Song"

这是它的输出方式:


         @{Song=The Story so Far; Album=R40 Live; Year=2015; Notes=Drum solo; interlude during Cygnus X-1}.Song
@{Song=Drumbastica; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo; interlude during Headlong Flight}.Album  2013   

         @{Song=Drumbastica; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo; interlude during Headlong Flight}.Song 
@{Song=Here It Is!; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo}.Album  2013

         @{Song=Here It Is!; Album=Clockwork Angels Tour; Year=2013; Notes=Drum solo}.Song
@{Song=Peke's Repose; Album=Clockwork Angels Tour; Year=2013; Notes=Guitar solo; lead-in to Halo Effect}.Album  2013

我需要这样的输出:

R40 Live (2015)
    The Story so Far
Clockwork Angels Tour (2013)
    Drumbastica
    Here It Is!
    Peke's Repose
    The Percussor
Clockwork Angels (2012)
    BU2B
...

(注意:请忽略上例中的颜色。) 在使用循环从 CSV 文件获取数据后,我想做的是在第一行显示带有年份的专辑名称,然后所有与该专辑有关的歌曲都按字母顺序排列在选项卡上专辑名称后的行。我该怎么做?

编辑:按照@BrettFord1999 的建议,我得到的输出显示如下:

R40 Live (2015)  
         The Story so Far
Clockwork Angels Tour (2013)
         Drumbastica
Clockwork Angels Tour (2013)
         Here It Is!
Clockwork Angels Tour (2013)
         Peke's Repose
Clockwork Angels Tour (2013)
         The Percussor
Clockwork Angels (2012)
         BU2B

但是,我只想显示一次相册,而不是多次。有没有办法显示每个专辑名称一次,而不是与它链接到的每首歌曲一起显示?

我会提供原始文件,但我不知道如何在此处共享它。我还是 Whosebug 的新手。


我会先尝试将值设置为一个变量,然后为您的输出操作它。

如果您能提供一份 CSV 副本,我可以进一步查看。

  $output = Import-CSV $DatabasePath -Delimiter "`t" | Sort-Object @{Expression= 'Year'; Descending = $true}, @{Expression='Song'; Ascending=$true}
    #$output
    $output | ForEach-Object {
        $album = $_.Album
        $year = $_.Year
        $year = $_.Song

        Write-Host "$Album ($Year)"
        etc...
        }

它比我预期的要复杂一些,因为如果用户输入文件路径,我还必须设置将格式化的歌曲列表输出到文件的功能,但代码现在输出歌曲列表,如上所示。这是完整的代码:

$prop1 =@{Expression= 'Year'; Descending = $true}
$prop2 =@{Expression='Album'; Ascending=$true}
$prop3 =@{Expression='Song'; Ascending=$true}

$tab = [char]9
$output = Import-CSV $DatabasePath -Delimiter "`t" | Sort-Object $prop1, $prop2, $prop3

    $test = @()
    $count = 0
    $output | ForEach-Object {
        $album = $_.Album
        $year = $_.Year
        $song = $_.Song
        if ($output[$count].album -like $output[$count -1].album){
           $test += "$tab$Song"
        } else{
            $test += "$Album ($Year)"
            $test += "$tab$Song"
        } $count++

    }  
    if ($path.Length -gt 0){
        $test | Out-File -Path $Path
    } else {
       $test | ForEach-Object {
           Write-Host $_
       }
    }