I am absolutely lost as to what is happening here. I am simply comparing the values of two numbers and returning the result. When I'm doing it in a custom property, the result is always False, even though the value is indeed greater. When I take out just the comparison into its own line after the Select-Object operation, the correct result, True, is displayed.
Code examples:
$var = Get-BrokerDesktop -Filter {HostedMachineName -eq $machineName} |
Select-Object -Property MachineName,
@{L="UptimeHours";E={(New-TimeSpan $_.LastHostingUpdateTime (Get-Date)).TotalHours}},
@{L='UptimeFormatted';E={'{0:%d} Days & {0:%h\:mm\:ss}' -f (New-TimeSpan $_.LastHostingUpdateTime (Get-Date))}},
@{L="ToRestart";E={[int]$_.UptimeHours -gt 24}},
RegistrationState
[int]$var.UptimeHours -gt 24
For the record, UptimeHours is 87 in this case. 87.3 before the integer cast.
I am beyond confused how two identical logical comparisons can return different values.
When using Select-Object
with calculate properties, the $_
inside the property expression refers to the current input object - which doesn't have an UptimeHours
property, otherwise you wouldn't need to create it in the first place :)
Either repeat the same time span calculation in both expressions:
...,@{L="ToRestart";E={[int](New-TimeSpan $_.LastHostingUpdateTime (Get-Date)).TotalHours -gt 24}},...
... or chain an extra Select-Object
command onto the pipeline so you can select the last property based on the value of the ones first introduced with Select-Object
:
... |Select-Object -Property MachineName,
@{L="UptimeHours";E={(New-TimeSpan $_.LastHostingUpdateTime (Get-Date)).TotalHours}},
@{L='UptimeFormatted';E={'{0:%d} Days & {0:%h\:mm\:ss}' -f (New-TimeSpan $_.LastHostingUpdateTime (Get-Date))}},
RegistrationState | Select-Object -Property *,@{L="ToRestart";E={[int]$_.UptimeHours -gt 24}}