Repository navigation
Description
Currently, Get-Date returns a PSObject wrapped DateTime.
PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs
Lines 315 to 323 in 4dc5512
| else | |
| { | |
| //output DateTime object wrapped in an PSObject with DisplayHint attached | |
| PSObject outputObj = new PSObject(dateToUse); | |
| PSNoteProperty note = new PSNoteProperty("DisplayHint", DisplayHint); | |
| outputObj.Properties.Add(note); | |
| WriteObject(outputObj); | |
| } |
This is usually not an issue, but in some instances it causes confusion or unexpected results. Take for example the following 2 pieces of code:
$d = Get-Date;
(ConvertFrom-Json (ConvertTo-Json $d)).ToLocalTime()vs
(ConvertFrom-Json (ConvertTo-Json (get-date))).ToLocalTime()The first will correctly reserailize the DateTime, but the second will result in the Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'ToLocalTime'. error.
This is due to PSObject black magic and Get-Date applying the DisplayHint PSNoteProperty. the JSON serializers in both Windows PowerShell and PowerShell Core serialize the outer PSObject and not the BaseObject. While that may or may not be an issue in the JSON cmdlets, this issue could pop its head up any other cmdlet where the outer PSObject would be evaluated instead of the inner DateTime.
I believe adding a None option to the -DisplayHint parameter would be sufficient to allow users to work around this where needed/desired. I haven't looked too deeply into the logic, but the idea would be that if this is present it would just send the raw DateTime object without wrapping it in a PSObject and applying the PSNoteProperty.
Activity
mklement0 commentedon Jan 7, 2018
In the case of
ConvertTo-Json, the presence of NoteProperty / ScriptProperty members should not make a difference, as I (now) argue in #5797, which requires fixingConvertTo-Json.As a workaround in the meantime, you can use
(Get-Date).psobject.BaseObjector direct construction via[datetime], but note that this only works if you pass this value via-InputObjecttoConvertTo-Json, not via the pipeline:In summary:
ConvertTo-Json, as described in ConvertTo-Json: unexpected behavior with objects that have ETS properties (NoteProperty, ScriptProperty) #5797.Get-Date.markekraus commentedon Jan 7, 2018
Michael Klement (@mklement0) The goal of this PR is NOT to interop with JSON serialization. It was only the example I used. There are other areas where this pops up. so it does need to be applied to
get-date.mklement0 commentedon Jan 8, 2018
Mark Kraus (@markekraus)
It seems to me that the real issue is that we sometimes want to get the unadorned-by-PS version of an object for any type.
(...).psobject.BaseObjectshould give you that for any type, but currently:(a) it doesn't fully, because ETS properties are apparently still present (the
DateTimeScriptProperty member, in the case of[datetime]instances) - I don't know whether that's by design, but I can certainly imagine scenarios where you don't even want the ETS properties.(b) such ETS properties may surface again when an unadorned instance is sent through the pipeline; contrast
ctj ([datetime]::now)with[datetime]::now | ctj; I've only seen it withConvertTo-Jsonat this point, but, as you state, it may affect others too.If these two aspects are clarified / fixed, there is no need for
-DisplayHint None, the generalization of which would require the highly impractical approach of adding analogous, but specific parameters to all cmdlets, along the lines ofGet-Content -NoProviderPropertiesto get unadorned string instances.SteveL-MSFT commentedon Jun 24, 2021
Closing this in favor of #5797