Skip to content

Add-History date behaviour changed from 7.1 to 7.2 - now requires least significant centred format.  #15949

Closed

Description

Prerequisites

Steps to reproduce

On a machine where the date format is not US

  1. Export history to a csv file with Get-History | Export-Csv -path $logfile -encoding unicode -NoTypeInformation
  2. Import the last item of the history with $histitem = Import-Csv -Path $logfile | Select-Object -last 1
  3. Load the item into the the history with $histitem | Add-History
  4. Change to US date format, with $histitem.EndExecutionTime = "08/18/2021 16:43:50"; $histitem.StartExecutionTime = "08/18/2021 16:43:50" - note that the imported fields are strings, not [datetime] objects.
  5. Import the modified item with $histitem | Add-History

Expected behavior

On a non-US machine the import at step 3 works , and the import at step 5 generates the message 
"Add-History: Cannot add history because the input object has a format that is not valid."

Actual behavior

On Windows PowerShell, PowerShell Core 6.x and Powershell 7 and 7.1 the local date format succeeds and US date format fails. 
On PowerShell 7.2 preview 8 Local date format fails and only US format succeeds.

Error details

�[92mException             : �[0m
    �[92mType    : �[0mSystem.IO.InvalidDataException
    �[92mMessage : �[0m�[31;1mCannot add history because the input object has a format that is not valid.
    �[92mHResult : �[0m-2146233087
�[92mTargetObject          : �[0m@{Id=520; CommandLine=$histitem | Add-History; ExecutionStatus=Completed; StartExecutionTime=18/08/2021 18:00:35; EndExecutionTime=18/08/2021 18:00:35; Duration=00:00:00.0826743}
�[92mCategoryInfo          : �[0mInvalidData: (@{Id=520; CommandLiÔǪn=00:00:00.0826743}:PSObject) [Add-History], InvalidDataException
�[92mFullyQualifiedErrorId : �[0mAddHistoryInvalidInput,Microsoft.PowerShell.Commands.AddHistoryCommand
�[92mInvocationInfo        : �[0m
    �[92mMyCommand        : �[0mAdd-History
    �[92mScriptLineNumber : �[0m1
    �[92mOffsetInLine     : �[0m13
    �[92mHistoryId        : �[0m105
    �[92mLine             : �[0m$histitem | Add-History
    �[92mPositionMessage  : �[0mAt line:1 char:13
                       + $histitem | Add-History
                       +             �[31;1m~~~~~~~~~~~
    �[92mInvocationName   : �[0mAdd-History
    �[92mCommandOrigin    : �[0mInternal
�[92mScriptStackTrace      : �[0mat <ScriptBlock>, <No file>: line 1
�[92mPipelineIterationInfo : �[0m

Environment data

�[32;1mName                           Value�[0m
�[32;1m----                           -----�[0m
PSVersion                      7.2.0-preview.8
PSEdition                      Core
GitCommitId                    7.2.0-preview.8
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Aug 18, 2021
  2. jhoneill commented on Aug 18, 2021

    @jhoneill
    Author

    Note the issue with ansi formatting on automatic get-error | clip and $psversionTable | clip both contain ansi sequences, but the error does ask for get-error verbatim

  3. iSazonov commented on Aug 19, 2021

    @iSazonov
    Collaborator

    Note the issue with ansi formatting on automatic get-error | clip and $psversionTable | clip both contain ansi sequences, but the error does ask for get-error verbatim

    I believe it was resolved in #15882

  4. iSazonov commented on Aug 19, 2021

    @iSazonov
    Collaborator

    Add-History always used Invariant culture. I guess Export-Csv was changed to use Current culture.

  5. jhoneill commented on Aug 19, 2021

    @jhoneill
    Author

    Add-History always used Current culture. I guess Export-Csv was changed to use Invariant culture

    Ilya (@iSazonov) Add history is not using current culture now. Export-CSV still exports the string as current culture and I get the problem exporting my history from 7.1 and importing it into 7.2 (the export is for completeness). Import-CSV doesn't recognize dates, so you get a string

    7.1 and prior a string in EndExecutionTime / StartExecutionTime must use current culture, 7.2 it must use invariant.

  6. iSazonov commented on Aug 19, 2021

    @iSazonov
    Collaborator

    I updated my comment. Add-History uses Invariant culture in 7.2.

  7. iSazonov commented on Aug 19, 2021

    @iSazonov
    Collaborator

    The regression comes from #12495.
    We should use another overload public static bool TryConvertTo<T>(object valueToConvert, IFormatProvider formatProvider, out T result)

    /cc Rain Sallow (/u/ta11ow) (@vexx32) Rob Holt (@rjmholt)

  8. added
    Issue-Bug
    on Aug 19, 2021
  9. added 3 commits that reference this issue on Aug 26, 2021
  10. ghost removed
    Needs-Triage
    on Aug 27, 2021
  11. added
    WG-Cmdlets
    and removed on Jul 17, 2026
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-BugResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions