Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 按唯一列和最新时间对Powershell阵列排序_Arrays_Powershell_Sorting_Unique - Fatal编程技术网

Arrays 按唯一列和最新时间对Powershell阵列排序

Arrays 按唯一列和最新时间对Powershell阵列排序,arrays,powershell,sorting,unique,Arrays,Powershell,Sorting,Unique,我将系统日志消息作为Heartbeat发送,并希望将日志文件显示为已排序的html(按主机和最新接收到的Heartbeat)。我的数组如下所示: Date | Host ---- | ---- 31.10.2017 16:59:37 | Host0815 31.10.2017 16:59:31 | Host2123 31.10.2017 16:59:31 |

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

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尝试了许多版本;排序对象-唯一等,但始终失败

发布的输出由以下内容生成:

$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 
有没有人能帮我,或者告诉我这是否可行

谢谢
载重线示例:

$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]
对象。我不知道你的数据是否如此(你可能需要这样做),但我的简短测试输出了你所期望的结果。

首先:感谢你花了这么多时间!我试着修改你的例子,结果成功了,只是做了一个“改进”:

也很管用,很不错!但现在我尝试将这些更改应用于我的脚本,但再次失败:

$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正在更改,因为随机创建了字符串):

带有排序示例的输出:

$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
有什么建议说明为什么手动填充时“相同”数组可以工作,但在我的版本中没有

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


多谢各位

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

$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

他导入html日志文件的方式似乎存在问题。这很可能,因此我的评论是需要转换为
[DateTime]
。我建议添加
|排序对象-属性“Date”-降序
,以确保按日期而不是主机名排序。如果您导入html,您需要将日期/时间强制转换为
[DateTime]
对象,以便按照我的预期进行排序。
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
$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