Select statement return a collection rather than individual item - Powershell

31 Views Asked by At

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.

0

There are 0 best solutions below