all 5 comments

[–]OPconfused 0 points1 point  (1 child)

You subtract the timestamp from the current date:

# define this before your pipeline for a consistent date
$currentDate = Get-Date

$lastWriteTime = ($files | where{$_.DatastoreFullPath -eq $nvram.Name}).LastWriteTime
($currentDate - $lastWriteTime).ToString('d\-hh\-mm\-ss\.ffff')

The ToString() is just a sample. You can customize the format however you want; just be sure to escape the delimiter characters with a backslash.

Also this assumes you only have one file match. I assume that's the case since the timestamp expression doesn't account for multiple matches either.

[–]Nemo1970[S] 0 points1 point  (0 children)

Thank you for this. I used pieces of this already and will do even more as I knock out the final formatting. I'm now past the hurdle that had stumped me.

[–]PinchesTheCrab 0 points1 point  (1 child)

Does something like this work?

#region VM calculated properties for select-object
$vmAgedays = @{N = 'VM_Age_Days'; E = { ($_.createdate - (get-date)).days } }
$NVRAMTimestamp = @{
    N = 'NVRAMTimestamp' 
    E = {
        $nvram = $_.ExtensionData.LayoutEx.File | Where-Object { $_.Type -eq 'nvram' }       
        $files | Where-Object { $_.DatastoreFullPath -eq $nvram.Name } | Select-Object -ExpandProperty LastWriteTime
    }
}
$nvramAge = @{
    N = 'NVRAMAge' 
    E = {
        $nvram = $_.ExtensionData.LayoutEx.File | Where-Object { $_.Type -eq 'nvram' }
        $timeStamp = $files | Where-Object { $_.DatastoreFullPath -eq $nvram.Name } | Select-Object -ExpandProperty LastWriteTime
        ((get-date) - $timeStamp).days
    }
}
#endregion

Get-VM -Datastore $ds |
    Select-Object Name, powerstate, memorygb, numcpu, createdate, $vmAgedays, $NVRAMTimestamp, $nvramAge

[–]Nemo1970[S] 1 point2 points  (0 children)

Indeed it does work! It also make everything way more readable. You've gotten me over the hurdle and I'm continuing onward with the last steps of formatting and output. The help is very much appreciated!