【问题标题】:Sorting Powershell Array by Unique Column and newest Time按唯一列和最新时间​​对 Powershell 数组进行排序
【发布时间】:2017-10-31 17:59:12
【问题描述】:

我将系统日志消息作为心跳发送,并希望将日志文件显示为排序的 html(按主机和最新收到的心跳)。我的数组如下所示:

Date                 |   Host       
----                 |   ----       
31.10.2017 16:59:37  |   Host0815      
31.10.2017 16:59:31  |   Host2123      
31.10.2017 16:59:31  |   Host1       
31.10.2017 16:59:31  |   Host0815      
31.10.2017 16:59:25  |   Host0815      
31.10.2017 16:59:25  |   Host2123      
31.10.2017 16:59:25  |   Host1       
31.10.2017 16:59:19  |   Host0815 

我不想要所有消息,只想要来自每个主机的最后一个消息。已经使用 Get-Unique 尝试了许多版本; Sort-Object -unique 等等,但一直失败。

发布的输出由以下人员生成:

$array = $array | Sort-Object Host, Date -Unique | Sort-Object Date -Descending

-unique 开关似乎没有效果。此外,如果我尝试按唯一主机排序,我可以工作,但我确实有“随机”日期值,而不是每个日期的最新值。

期望的输出:

Date                 |   Host       
----                 |   ----       
31.10.2017 16:59:37  |   Host0815      
31.10.2017 16:59:31  |   Host2123      
31.10.2017 16:59:31  |   Host1 

有人可以帮助我,甚至可以告诉我这是否可能吗?

谢谢!
载重线

【问题讨论】:

  • 如果您要导入 HTML,您需要将日期/时间转换为 [DateTime] 对象,以便按照我的预期进行排序。

标签: arrays powershell sorting unique


【解决方案1】:

例子:

$list = @(
  [PSCustomObject] @{
    Date = [DateTime] "10/31/2017 16:59:37"
    Host = "Host0815"
  }
  [PSCustomObject] @{
    Date = [DateTime] "10/31/2017 16:59:31"
    Host = "Host2123"
  }
  [PSCustomObject] @{
    Date = [DateTime] "10/31/2017 16:59:31"
    Host = "Host1"
  }
  [PSCustomObject] @{
    Date = [DateTime] "10/31/2017 16:59:31"
    Host = "Host0815"
  }
  [PSCustomObject] @{
    Date = [DateTime] "10/31/2017 16:59:25"
    Host = "Host0815"
  }
  [PSCustomObject] @{
    Date = [DateTime] "10/31/2017 16:59:25"
    Host = "Host2123"
  }
  [PSCustomObject] @{
    Date = [DateTime] "10/31/2017 16:59:25"
    Host = "Host1"
  }
  [PSCustomObject] @{
    Date = [DateTime] "10/31/2017 16:59:19"
    Host = "Host0815"
  }
)

$list | Sort-Object Host -Unique | Sort-Object Date -Descending

输出:

Date                  Host
----                  ----
10/31/2017 4:59:37 PM Host0815
10/31/2017 4:59:31 PM Host1
10/31/2017 4:59:31 PM Host2123

我创建了示例输入数据 ($list),其中 Date 列是实际的 [DateTime] 对象。我不知道您的数据是否属于这种情况(您可能需要这样转换),但我的简短测试输出了您的预期。

【讨论】:

  • 他在导入 html 日志文件时似乎存在问题。
  • 这很可能,因此我评论需要转换为[DateTime]
  • 我建议添加 | Sort-Object -Property 'Date' -Descending 以确保它按日期而不是主机名排序。
【解决方案2】:

PowerShell 唯一性并不是为了处理这种复杂性而构建的。您可以先按主机分组,然后您可以按主机进行排序和选择:

$array | group Host | % {$_ | select -ExpandProperty group | sort Date -Descending | select -First 1}


Date                Host    
----                ----
31.10.2017 16:59:37 Host0815    
31.10.2017 16:59:31 Host2123    
31.10.2017 16:59:31 Host1

【讨论】:

    【解决方案3】:

    首先:感谢您的宝贵时间!我试图修改你的例子,它奏效了,只是做了一个“改进”:

    $list = @(
    [PSCustomObject] @{
    Date = [DateTime]::ParseExact("31.10.2017 16:59:37","dd.MM.yyyy HH:mm:ss", $null)
    Host = "Host1234"
    }...
    

    也可以,非常好!但是现在我尝试将这些更改适应我的脚本并再次失败:

    $data = Get-Content "C:\heartbeat-log.txt" -tail 50
    
    $array.Clear()
    
            ForEach ($line in $data)
            {
                $string = ($line -split "SPACER,") -split ","
    
                  $array += @(
                   [PSCustomObject] @{
                    Date = [DateTime]::ParseExact($string[1], "dd.MM.yyyy HH:mm:ss", $null)
                    SN = [string]$string[2]
                    Host = [string]$string[3]
                    Msg = [string]$string[4]
                    Status = [string]$string[5]
                    CpuCores = [int]$string[6]
    
                  }
                  )
    
            }
    

    这就是我的输入数据的样子(SN 正在改变,因为随机创建的字符串):

    SPACER,31.10.2017 21:03:23,MZ58XJ6UBH,Host1,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:03:23,MZ58XJ6UBH,Host2123,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:03:29,MZ58XJ6UBH,Host0815,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:03:29,MZ58XJ6UBH,Host1,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:03:29,MZ58XJ6UBH,Host2123,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:03:35,MZ58XJ6UBH,Host0815,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:03:35,MZ58XJ6UBH,Host1,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:03:35,MZ58XJ6UBH,Host2123,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:08:55,PKRTVWLOMJ,Host0815,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:08:55,PKRTVWLOMJ,Host1,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:08:55,PKRTVWLOMJ,Host2123,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:09:01,PKRTVWLOMJ,Host0815,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:09:01,PKRTVWLOMJ,Host1,CustomMsg,CustomStatus,2
    SPACER,31.10.2017 21:09:01,PKRTVWLOMJ,Host2123,CustomMsg,CustomStatus,2
    

    输出与您的排序示例:

    $array | Sort-Object Host -Unique | Sort-Object Date -Descending | ft
    
    Date                    SN            Host        Msg        Status        CpuCores
    ----                    --            ----        ---        ------        --------
    31.10.2017 21:03:23     MZ58XJ6UBH    Host1       CustomMsg  CustomStatus  2
    31.10.2017 19:54:06     XM6YCR5ED2    Host2123    CustomMsg  CustomStatus  2
    31.10.2017 19:50:46     QJKWLD7901    Host0815    CustomMsg  CustomStatus  2
    

    期望:

    Date                    SN            Host        Msg        Status        CpuCores
    ----                    --            ----        ---        ------        --------
    31.10.2017 21:09:01     PKRTVWLOMJ    Host1       CustomMsg  CustomStatus  2
    31.10.2017 21:09:01     PKRTVWLOMJ    Host2123    CustomMsg  CustomStatus  2
    31.10.2017 21:09:01     PKRTVWLOMJ    Host0815    CustomMsg  CustomStatus  2
    

    任何建议为什么“相同”数组在手动填充时工作但在我的版本中没有?

    (希望这次我的帖子格式正确;))

    非常感谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多