0

I'm trying to grab and old Archive date to compare it to today to see when was the last time data was written to a certain table and present a report. However, I'm not able to break the LastArchivedDate collection and get individual values.

$oldFileDate = ((Get-Date).AddDays(-1)).ToString('MM-dd-yyyy')
$oldReportFilepath =  $reportResultFolder + 'ZeroJournalReport_'+ $oldFileDate + '.csv'
$getOlderFile = Import-csv $oldReportFilepath
$oldInfo = $getOlderFile | Select VaultName,LastArchivedDate

$vaultDifference = $AllVaultNamesList| ForEach-Object{
    $LastArchivedDate = $archivedDate
    if($journaledToday -notcontains $_ -and $inactArchives_List -notcontains $_ ){
        $currVault = $_
        if($oldInfo.VaultName -contains $currVault){
            Write-host 'We are above the ForEach with value:'  $currVault
            $LastArchivedDate = $getOldInfo|select -ExpandProperty LastArchivedDate  | Where-Object{$getOldInfo.VaultName -eq $currVault} 
        Write-Host "This is your last Archived date: " $LastArchivedDate 
        }
        $endDate= (Get-Date $archivedDate)
        $startDate = (Get-Date $LastArchivedDate)
        $daysDiff = New-TimeSpan -Start $startDate -End $endDate
        $numOfDays = $daysDiff.Days
        $numOfDay = switch($numOfDays){
            #0 {"Today"; break}
            0{"A day ago";break }
            1{"2 days ago";break }
            2{"3 days ago";break }
            Default{
                "More than 3 days"; break
            }
        }
        $vaultDifferenceItem = [PSCustomObject][Ordered]@{
            'ArchivedDate' = $archivedDate
            'VaultName' = $_
            'MessagesArchived' = 0
            'LastArchivedDate' = $LastArchivedDate
            'LastTimeArchived' = $numOfDay
        }
    $vaultDifferenceItem
    }
}
$vaultDifference| Select-Object ArchivedDate, VaultName,MessagesArchived,LastArchivedDate| Where-Object {$_.VaultName -ne $null}

This is the line of code I execute expecting the correct return date.

$LastArchivedDate = $oldInfo|select -ExpandProperty LastArchivedDate | Where-Object{$getOldInfo.VaultName -eq $currVault}

But instead it returns the following: This is your last Archived date: 2023-08-05 2023-08-05 2023-08-04 2023-08-04 2023-08-05 2023-08-05 2023-08-05 2023-08-04 2023-08-04 2023-08-04 Rather than the individual date.

Greg
  • 13
  • 3
  • I’ve not read your full code sample (the formatting is a bit screwy, which makes it hard to grok) but try swapping the order of the ```select-object``` and ```where-object``` in the pipeline, and change ```$getOldInfo.VaultName``` to ```$_.VaultName``` in the ```where-object``` – mclayton Aug 07 '23 at 04:59
  • Is this the complete script? You never assign anything to `$getOldInfo` – Mathias R. Jessen Aug 07 '23 at 12:50
  • This worked! Thanks for your help. I’ve not read your full code sample (the formatting is a bit screwy, which makes it hard to grok) but try swapping the order of the select-object and where-object in the pipeline, and change $getOldInfo.VaultName to $_.VaultName in the where-object – mclayton – Greg Aug 07 '23 at 13:51

0 Answers0