Skip to content

Add "None" option to Get-Date -DisplayHint #5676

Closed

Description

Currently, Get-Date returns a PSObject wrapped DateTime.

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

  1. added
    Issue-Discussion
    Issue-Enhancement
    on Dec 12, 2017
  2. mklement0 commented on Jan 7, 2018

    @mklement0
    Contributor

    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 fixing ConvertTo-Json.

    As a workaround in the meantime, you can use (Get-Date).psobject.BaseObject or direct construction via [datetime], but note that this only works if you pass this value via -InputObject to ConvertTo-Json, not via the pipeline:

    # OK - via -InputObject
    ConvertTo-Json (get-date).psobject.baseobject 
    ConvertTo-Json ([datetime]::now)
    
    # !! BROKEN - via pipelin
    (get-date).psobject.baseobject | ConvertTo-Json
    [datetime]::now | ConvertTo-Json

    In summary:

  3. markekraus commented on Jan 7, 2018

    @markekraus
    ContributorAuthor

    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.

  4. mklement0 commented on Jan 8, 2018

    @mklement0
    Contributor

    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.BaseObject should give you that for any type, but currently:

    (a) it doesn't fully, because ETS properties are apparently still present (the DateTime ScriptProperty 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 with ConvertTo-Json at 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 of Get-Content -NoProviderProperties to get unadorned string instances.

  5. SteveL-MSFT commented on Jun 24, 2021

    @SteveL-MSFT
    Member

    Closing this in favor of #5797

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionIssue-EnhancementWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions