1

i am sending Syslog Messages as Heartbeat and want to display the logfile as sorted html (by host and latest received heartbeat). My array looks like this:

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 

I dont want all the Messages, but just the last one from each host. Already tried many versions with Get-Unique; Sort-Object -unique and so on but failed all the time.

The posted output is produced by:

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

The -unique Switch seems to have no effect. Also if i try so Sort by Unique Hosts i works, but i do have "random" Date Values and not the latest by each.

Desired Output:

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

Can someone help me or even tell me if this is possible?

Thanks!
Loadline

1
  • If you are importing HTML, you will need to cast the dates/times as [DateTime] objects for sorting to work as I think you are expecting. Commented Oct 31, 2017 at 18:36

3 Answers 3

1

Example:

$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

Output:

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

I created the sample input data ($list) with the Date column being an actual [DateTime] object. I don't know whether that's the case with your data (you may need to cast as such), but my short test outputs what you were expecting.

Sign up to request clarification or add additional context in comments.

3 Comments

It would appear there's an issue with however he's importing the html log file.
This is likely, hence my comment of the need to cast as [DateTime].
I'd suggest adding | Sort-Object -Property 'Date' -Descending to make sure it's sorted on date rather than hostname.
1

PowerShell unique-ing isn't built to handle this complexity. You can group by host first, then you can sort and select on a per host basis:

$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

Comments

0

first of all: Thanks for all your time! I tried to adapt your example and it worked, just did one "improvement":

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

works too, which is pretty nice! But now i tried do adapt these changes to my Script and failed again:

$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]

              }
              )

        }

This is how my input Data looks like (SN is changing because random created String):

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

Output with the your sort example:

$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

Expectation:

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

Any suggestions why the "same" Array is working when manually filled but not in my version?

(hopefully formatted my post correct this time ;) )

Thank you very much!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.