Powershell to select the last result based on the per object (Jobname =Specification)

Kalaimani Thirupathi 411 Reputation points
2021-02-21T13:08:33.093+00:00

Dear All,

I'm developing HPDP daily report, able to get the result but the same job run multiple time in one day due to failure

Currently, I'm looking to get only the last result in the last 24 hours

$onlyLastEp =Get-ListSessions
foreach ($obj in $onlyLastEp) {
$obj |?{$obj.Specification } | Sort-Object EndTime -Descending | Select-Object -Property "Session Type", Specification,Status | Select-Object -First 1 | Export-Csv C:\Temp\report.csv -Append
}

this working but still getting the duplicate

available membership :

PS C:\Windows\system32> Get-ListSessions |gm

TypeName: Selected.System.Management.Automation.PSCustomObject
Name MemberType Definition


Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Duration NoteProperty System.TimeSpan Duration=00:02:51
End Time NoteProperty System.DateTime End Time=2/20/2021 7:32:54 PM
Errors NoteProperty System.Int32 Errors=0
Files NoteProperty System.Int32 Files=309
GB Written NoteProperty System.Double GB Written=1.55
Media NoteProperty System.Int32 Media=5
Mode NoteProperty System.String Mode=diff
Objects NoteProperty System.Int32 Objects=5
Queuing NoteProperty System.String Queuing=0:00
Session ID NoteProperty System.String Session ID=2021/02/20-7
Session Type NoteProperty System.String Session Type=Backup
Specification NoteProperty System.String Specification=MSSQL H_Backup
Start Time NoteProperty System.DateTime Start Time=2/20/2021 7:30:03 PM
Status NoteProperty System.String Status=Completed
Warnings NoteProperty System.Int32 Warnings=0

PS C:\Windows\system32>
PS C:\Windows\system32>

Can someone help me with this?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2021-02-21T15:22:14.03+00:00

    If you only want to most recent object, try this:

    Get-ListSessions | 
        Where-Object { $_.Specification } | 
            Sort-Object EndTime -Descending |
                Select-Object -First 1 |
                    Select-Object -Property "Session Type", Specification, Status | 
                        Export-Csv C:\Temp\report.csv -Append
    

    The "Specification" property looks to be a string object. Are you simply testing to see if there's a non-empty string present? Is there any chance that you may encounter a string that's composed of only "space" characters?


0 additional answers

Sort by: Most helpful