Title: Compiled cmdlets - unlike functions - perform culture-sensitive type conversion during parameter binding · Issue #6989 · PowerShell/PowerShell · GitHub
Open Graph Title: Compiled cmdlets - unlike functions - perform culture-sensitive type conversion during parameter binding · Issue #6989 · PowerShell/PowerShell
X Title: Compiled cmdlets - unlike functions - perform culture-sensitive type conversion during parameter binding · Issue #6989 · PowerShell/PowerShell
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 ...
Open Graph 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...
X 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...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/6989
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Compiled cmdlets - unlike functions - perform culture-sensitive type conversion during parameter binding","articleBody":"Note: Fixing this inconsistency is probably not an option so as not to break existing code.\r\nIf so, it should be _documented_.\r\n\r\nPowerShell 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_.\r\n\r\nBy contrast, a _culture-sensitive_ conversion is performed during parameter binding for _compiled cmdlets_.\r\n\r\n\r\nSteps to reproduce\r\n------------------\r\n\r\n```powershell\r\n# Create a cmdlet that accepts a [datetime] argument.\r\nAdd-Type @'\r\n using System;\r\n using System.Management.Automation;\r\n [Cmdlet(\"Get\", \"Foo_Cmdlet\")]\r\n public class GetFooCmdlet : Cmdlet {\r\n\r\n [Parameter(Position=0)]\r\n public DateTime Date { get; set; }\r\n\r\n protected override void ProcessRecord() {\r\n WriteObject(Date); \r\n }\r\n }\r\n'@ -PassThru | % Assembly | Import-Module\r\n\r\n# Define a seemingly equivalent function.\r\nfunction Get-Foo_Func {\r\n param(\r\n [DateTime] $Date\r\n )\r\n process {\r\n $Date\r\n }\r\n}\r\n\r\n$prevCulture = [cultureinfo]::CurrentCulture\r\n[cultureinfo]::CurrentCulture = 'de-DE'\r\n\r\n# This German-format date string doesn't work with the invariant culture.\r\n# E.g., [datetime] '19-06-2018' breaks.\r\n$dateStr = '19-06-2018'\r\n\r\n# Pass the string to a [datetime]-typed parameter to force implicit conversion.\r\n\r\n# Function: INVARIANT CULTURE is applied -> breaks.\r\nGet-Foo_Func $dateStr\r\n\r\n# Cmdlet: CURRENT CULTURE is applied -> succeeds.\r\nGet-Foo_Cmdlet $dateStr\r\n\r\n[cultureinfo]::CurrentCulture = $prevCulture\r\n```\r\n\r\nExpected behavior\r\n-----------------\r\n\r\n_Both_ calls (`Get-Foo_Func`) and (`Get-Foo_Cmdlet`) should fail with:\r\n\r\n```none\r\nGet-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.\"\r\n```\r\n\r\nActual behavior\r\n---------------\r\n\r\n`Get-Foo_Func` fails, and `Get-Foo_Cmdlet` succeeds:\r\n```none\r\nGet-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.\"\r\n#...\r\n\r\nDienstag, 19. Juni 2018 00:00:00\r\n```\r\n\r\nEnvironment data\r\n----------------\r\n\r\n\u003c!-- provide the output of $PSVersionTable -->\r\n\r\n```powershell\r\nPowerShell Core v6.0.2 on macOS 10.13.4\r\nPowerShell Core v6.0.2 on Ubuntu 16.04.4 LTS\r\nPowerShell Core v6.0.2 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)\r\nWindows PowerShell v5.1.16299.251 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2018-06-04T17:17:01Z","url":"https://github.com/PowerShell/PowerShell/issues/6989"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:75c29a6a-66ce-fd97-529d-b4dfe1d4f247 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 97C2:1E2D6E:70813:9CA4D:6AFD593C |
| html-safe-nonce | 7b992794d767bbcd2fb8d14fe22381858d1aa15ca2064e34022bf89fcf114258 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5N0MyOjFFMkQ2RTo3MDgxMzo5Q0E0RDo2QUZENTkzQyIsInZpc2l0b3JfaWQiOiI4MTMyNjg3OTQ3MzUxNTQ2MTcyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9363919f978b0cb02604901159885b3f1d0c0866b4b0b2ae6a787faad0042880 |
| hovercard-subject-tag | issue:329144722 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/6989/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1e59b9d593473d1bddd0981f8404f79029d57c2b46f8f24284d7f0f23f5ea405/PowerShell/PowerShell/issues/6989 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1e59b9d593473d1bddd0981f8404f79029d57c2b46f8f24284d7f0f23f5ea405/PowerShell/PowerShell/issues/6989 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 66653c2e55a6fff2930be1bdf07ee4f8aa137e3956d056c822563be6f878755e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b6f01f7cb9f35f8c09949773241a3d49dd04b340 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width