Title: Explicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable · Issue #3483 · PowerShell/PowerShell · GitHub
Open Graph Title: Explicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable · Issue #3483 · PowerShell/PowerShell
X Title: Explicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable · Issue #3483 · PowerShell/PowerShell
Description: Currently, preference variables, such as $VerbosePreference, are only predefined in the global scope, where they are type-constrained, such as to [System.Management.Automation.ActionPreference], which ensures that only valid values can b...
Open Graph Description: Currently, preference variables, such as $VerbosePreference, are only predefined in the global scope, where they are type-constrained, such as to [System.Management.Automation.ActionPreference], wh...
X Description: Currently, preference variables, such as $VerbosePreference, are only predefined in the global scope, where they are type-constrained, such as to [System.Management.Automation.ActionPreference], wh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/3483
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Explicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable","articleBody":"Currently, preference variables, such as `$VerbosePreference`, are only predefined in the _global_ scope, where they are type-constrained, such as to `[System.Management.Automation.ActionPreference]`, which ensures that only valid values can be assigned to them. \r\nSome preference variables, such as `$OFS`, aren't even predefined in the global scope, although their type (and default value) is known, as described in `Get-Help about_Preference_Variables`.\r\n\r\nBy contrast, assigning to a preference variable in any descendant scope implicitly creates a new variable that is not type-constrained, which effectively allows assigning _invalid values_.\r\n\r\nTherefore, assigning invalid values in _scripts_ may go unnoticed. \r\n(It seems that PowerShell quietly applies a given preference variable's default value in the event of encountering an invalid value.)\r\n\r\nSteps to reproduce\r\n------------------\r\n\r\n```powershell\r\n$VerbosePreference = 'ThisIsAnInvalidValue'\r\n\u0026 { $VerbosePreference = 'ThisIsAnInvalidValue' }\r\n```\r\n\r\nExpected behavior\r\n-----------------\r\n\r\nIdeally, _both_ commands would complain about the inability to convert string `'ThisIsAnInvalidValue'` to type `[System.Management.Automation.ActionPreference]`\r\n\r\n\r\nActual behavior\r\n---------------\r\n\r\n* The first command fails, as desired, because the top-level `$VerbosePreference` is type-constrained to `[System.Management.Automation.ActionPreference]`, and converting string `'ThisIsAnInvalidValue'` to that type fails.\r\n\r\n* The 2nd command is quietly accepted, because assigning to `$VerbosePreference` in the child scope created by `\u0026` implicitly creates a new, local variable that isn't type-constrained, so it effectively becomes a `[string]` value that cannot be converted to a legal `[System.Management.Automation.ActionPreference]` value.\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```powershell\r\nPowerShell Core v6.0.0-alpha (v6.0.0-alpha.17) on Darwin Kernel Version 16.5.0: Fri Mar 3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-04-04T22:49:59.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/3483/PowerShell/issues/3483"}
| 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:3d9e6173-29b3-2695-4917-293206fafc53 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B7DC:23B29E:3F8993:53CFB2:6A5B8A37 |
| html-safe-nonce | e945d4354f3328925b8fa8022b2e390db351a45a529c2a1de89a1d903652fbc1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCN0RDOjIzQjI5RTozRjg5OTM6NTNDRkIyOjZBNUI4QTM3IiwidmlzaXRvcl9pZCI6IjUzNDA2OTQ1NTMzNTExMjk2NTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 89e1f80a528e0ee8d4100380146f25bbd8ef78fee4a11fad950f97826f9d56e3 |
| hovercard-subject-tag | issue:219408965 |
| 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/3483/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6aad063d2554115d85e9723f3cf695241309028f11d43aa535df75e5c63872a4/PowerShell/PowerShell/issues/3483 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6aad063d2554115d85e9723f3cf695241309028f11d43aa535df75e5c63872a4/PowerShell/PowerShell/issues/3483 |
| og:image:alt | Currently, preference variables, such as $VerbosePreference, are only predefined in the global scope, where they are type-constrained, such as to [System.Management.Automation.ActionPreference], wh... |
| 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 | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width