Search code examples
powershellpowershell-2.0powershell-3.0powershell-4.0

Comparing Dates List with MAX Date


I have an output $a and $b with MAX date as shown below: I would like to compare both $a and $b which contains dates and get the results. The $a and $b will change on a daily basis. The goal is to get everything after the MAX Date everyday. The MAX date changes daily as new data is loaded with date and $a changes daily to.

This is how $a is derived:

$access_token ="Access_Token"

$URI =  "https://XXXXX"
$headers = @{“authorization” = “Bearer $access_token”} 
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$result = Invoke-RestMethod -Uri $URI -Headers $headers -ContentType $ContentType |ConvertTo-Json
$a = $Result|ConvertFrom-Json| Select -ExpandProperty Forms
$a= 

id          date
--------    -----------
Person 1    01/02/2017 10:59:15
Person 2    02/03/2017 13:10:19
Person 3    04/05/2017 11:11:12
Person 4    10/10/2017 10:42:19
Person 5    10/10/2017 13:34:58
$b= 

MAX_Date
02/03/2017 13:10:19 
Desired results:

id          date
--------    -----------
Person 3    04/05/2017 11:11:12
Person 4    10/10/2017 10:42:19
Person 5    10/10/2017 13:34:58

Solution

  • As Mathias said, we need more details. But to give you a head start the following would work:

    $A = @(
        [PSCustomObject]@{
            id   = 'Person 1'
            date = [DateTime]'01/02/2017 10:59:15'
        }
        [PSCustomObject]@{
            id   = 'Person 2'
            date = [DateTime]'02/03/2017 13:10:19'
        }
        [PSCustomObject]@{
            id   = 'Person 3'
            date = [DateTime]'04/05/2017 11:11:12'
        }
        [PSCustomObject]@{
            id   = 'Person 4'
            date = [DateTime]'10/10/2017 10:42:19'
        }
        [PSCustomObject]@{
            id   = 'Person 5'
            date = [DateTime]'10/10/2017 13:34:58'
        }
    )
    
    $B = @{
        MAX_Date = [DateTime]'02/03/2017 13:10:19 '
    }
    
    $A | Where-Object { $_.date -gt $B.MAX_Date } | Sort-Object id