将日期列表与MAX日期进行比较

时间:2019-09-05 09:45:27

标签: powershell powershell-2.0 powershell-3.0 powershell-4.0

我有一个输出$ a和$ b,具有最大日期,如下所示:我想比较包含日期的$ a和$ b并获取结果。 $ a和$ b每天都会更改。目标是每天在“最大日期”之后获取所有内容。 MAX日期每天都会更改,因为新数据会加载日期,而$ a每天都会更改。

这是$ a的导出方式:

$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

2 个答案:

答案 0 :(得分:1)

正如Mathias所说,我们需要更多细节。但是,为您起步,可以进行以下操作:

$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

答案 1 :(得分:0)

#First we need to cast this string into a date so it will compare properly.
$Max_Date = Get-Date $b.MAX_Date
#Then we need an array to store the objects that we want to keep
$Dates_to_Keep = ()
#Now we can iterate through the array and check the date on each object.
Foreach ($person in $a){
    #Again, casting this string as a date for accurate comparison. 
    $date = Get-Date $person.date
    If ($date -ge $Max_Date){
         $Dates_to_Keep = $Dates_to_Keep + $person
    }
}
Write-Host $Dates_to_Keep

根据OP中提供的信息,这是我的最佳猜测。