首先:感谢您的宝贵时间!我试图修改你的例子,它奏效了,只是做了一个“改进”:
$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
任何建议为什么“相同”数组在手动填充时工作但在我的版本中没有?
(希望这次我的帖子格式正确;))
非常感谢!