Skip to content

Compiled cmdlets - unlike functions - perform culture-sensitive type conversion during parameter binding #6989

Closed

Description

Note: Fixing this inconsistency is probably not an option so as not to break existing code.
If so, it should be documented.

PowerShell by design applies the invariant culture when converting from a string, which applies to both explicit conversions with casts and implicit ones during parameter binding in functions.

By contrast, a culture-sensitive conversion is performed during parameter binding for compiled cmdlets.

Steps to reproduce

# Create a cmdlet that accepts a [datetime] argument.
Add-Type @'
  using System;
  using System.Management.Automation;
  [Cmdlet("Get", "Foo_Cmdlet")]
  public class GetFooCmdlet : Cmdlet {

    [Parameter(Position=0)]
    public DateTime Date { get; set; }

    protected override void ProcessRecord() {
      WriteObject(Date);      
    }
  }
'@ -PassThru | % Assembly | Import-Module

# Define a seemingly equivalent function.
function Get-Foo_Func {
  param(
    [DateTime] $Date
  )
  process {
    $Date
  }
}

$prevCulture = [cultureinfo]::CurrentCulture
[cultureinfo]::CurrentCulture = 'de-DE'

# This German-format date string doesn't work with the invariant culture.
# E.g., [datetime] '19-06-2018' breaks.
$dateStr = '19-06-2018'

# Pass the string to a [datetime]-typed parameter to force implicit conversion.

# Function: INVARIANT CULTURE is applied -> breaks.
Get-Foo_Func $dateStr

# Cmdlet: CURRENT CULTURE is applied -> succeeds.
Get-Foo_Cmdlet $dateStr

[cultureinfo]::CurrentCulture = $prevCulture

Expected behavior

Both calls (Get-Foo_Func) and (Get-Foo_Cmdlet) should fail with:

Get-Foo_Func : Cannot process argument transformation on parameter 'Date'. Cannot convert value "19-06-2018" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

Actual behavior

Get-Foo_Func fails, and Get-Foo_Cmdlet succeeds:

Get-Foo_Func : Cannot process argument transformation on parameter 'Date'. Cannot convert value "19-06-2018" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
#...

Dienstag, 19. Juni 2018 00:00:00

Environment data

PowerShell Core v6.0.2 on macOS 10.13.4
PowerShell Core v6.0.2 on Ubuntu 16.04.4 LTS
PowerShell Core v6.0.2 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
Windows PowerShell v5.1.16299.251 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)

Activity

  1. rjmholt commented on Jun 4, 2018

    @rjmholt
    Collaborator

    One question is: should these both fail, or both work? It would make sense to me if they both worked in a given locale, although I can imagine that making scripts much harder to make portable.

  2. mklement0 commented on Jun 4, 2018

    @mklement0
    ContributorAuthor

    Rob Holt (@rjmholt):

    Given that in the vast majority of to-and-from string conversion cases PowerShell - by design - applies the invariant culture, I think both should fail.

    For instance, the following fails, irrespective of what the current culture is:

    [datetime] '19-06-2018'  # always fails

    To get culture-sensitive behavior in this case, you need to call a .NET method directly:

    [datetime]::Parse('19-06-2018')  # only works in certain cultures; e.g., 'de-DE'
    [datetime]::Parse('19-06-2018', [cultureinfo] 'de-DE') # OK 

    Note that string interpolation also always applies culture-invariant behavior:

    "$(1.2)"  # *always* yields '1.2'

    The issue at hand is another instance of written-in-PowerShell code behaving correctly (as far as I can tell), in contrast with compiled cmdlets.
    Unfortunately, when another such inconsistency was recently resolved, the wrong behavior was chosen - see #2035.

  3. added
    WG-Engine
    Issue-Discussion
    on Jun 5, 2018
  4. PetSerAl commented on Jun 24, 2018

    @PetSerAl
    Contributor

    Looks like a dup #3348.

  5. mklement0 commented on Jun 24, 2018

    @mklement0
    ContributorAuthor

    PetSerAl:

    Indeed it is, thank you; I'm sorry to hear that it was decided then that it wouldn't be fixed.

    I'm hoping this can be revisited.

  6. mklement0 commented on Apr 16, 2025

    @mklement0
    ContributorAuthor

    Note:

    This culture-sensitive behavior seems to be type-specific:

    For instance, it occurs with a [datetime]-typed parameter but not with an [int] parameter.

    As such, this inconsistent behavior seems to be the same as the one exhibited by the -as operator:

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-DiscussionResolution-DuplicateWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions