{"action":"created","issue":{"url":"https://api.github.com/repos/PowerShell/PowerShell/issues/7944","repository_url":"https://api.github.com/repos/PowerShell/PowerShell","labels_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/7944/labels{/name}","comments_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/7944/comments","events_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/7944/events","html_url":"https://github.com/PowerShell/PowerShell/issues/7944","id":366851753,"node_id":"MDU6SXNzdWUzNjY4NTE3NTM=","number":7944,"title":"$ExecutionContext.InvokeCommand.ExpandString does not work as expected in Modules","user":{"login":"Line40","id":5467899,"node_id":"MDQ6VXNlcjU0Njc4OTk=","avatar_url":"https://avatars2.githubusercontent.com/u/5467899?v=4","gravatar_id":"","url":"https://api.github.com/users/Line40","html_url":"https://github.com/Line40","followers_url":"https://api.github.com/users/Line40/followers","following_url":"https://api.github.com/users/Line40/following{/other_user}","gists_url":"https://api.github.com/users/Line40/gists{/gist_id}","starred_url":"https://api.github.com/users/Line40/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Line40/subscriptions","organizations_url":"https://api.github.com/users/Line40/orgs","repos_url":"https://api.github.com/users/Line40/repos","events_url":"https://api.github.com/users/Line40/events{/privacy}","received_events_url":"https://api.github.com/users/Line40/received_events","type":"User","site_admin":false},"labels":[{"id":401720735,"node_id":"MDU6TGFiZWw0MDE3MjA3MzU=","url":"https://api.github.com/repos/PowerShell/PowerShell/labels/Area-Engine","name":"Area-Engine","color":"5319e7","default":false},{"id":423526741,"node_id":"MDU6TGFiZWw0MjM1MjY3NDE=","url":"https://api.github.com/repos/PowerShell/PowerShell/labels/Issue-Discussion","name":"Issue-Discussion","color":"fc2929","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-10-04T15:40:09Z","updated_at":"2018-10-05T19:55:12Z","closed_at":null,"author_association":"NONE","body":"When used inside a module function `$ExecutionContext.InvokeCommand.ExpandString` only works for global variables. However using `$PSCmdLet.GetVariableValue` also works for Script Scope variables. I think they both should work the same way, or there should be some other way to make a module function inherit the execution context of the caller.\r\nThe issue came up when I tried to wrap `ExpandString` and `[System.Environment]::ExpandEnvironmentVariables` in a neat Cmdlet inside our inhouse helper module. Spent the last 5 hours trying to figure out a way to make it work, found some solutions but all are sort of ugly.\r\nI thought of two approaches to cleanly solve this issue:\r\n- a new option for `CmdletBinding`, like `[CmdletBinding(InheritExecutionContext)]`\r\n- some way to pass the `$ExecutionContext`, or the `$Executioncontext.InvokeCommand` property to a function/cmdlet.\r\n\r\nI know that dotsourcing the script works, but that is not what I would like, since it can't be in a module then.\r\nWhat also kind of works is to add the script to the `ScriptsToProcess` section in the modules *.psd1* file, but that's also not very tidy, since it prevents the user from using `Remove-Module` to fully remove the module. The script will remain as an extra module.\r\n\r\nA simple sample to demonstrate:\r\n------------------\r\n\r\nCreate a new Module, *ExpandStringIssue* and add a file *ExpandStringIssue.psm1* with the following content: \r\n```\r\nfunction ExpandStringIssue {\r\n[CmdletBinding()]\r\nParam( \r\n   [Parameter(Mandatory=$True,ValueFromRemainingArguments=$True,ValueFromPipeline=$True)]$Value\r\n)\r\nPROCESS {\r\n    [System.Environment]::ExpandEnvironmentVariables( $ExecutionContext.InvokeCommand.ExpandString( $Value ) )\r\n    $PSCmdlet.GetVariableValue( $Value.Trim( '$' ) )\r\n}\r\n}\r\n```\r\nCreate a script file named *ShowExpandStringIssue.ps1* with the following content\r\n```\r\nimport-module ExpandStringIssue\r\n\r\nNew-Variable -Scope Script -Name MyVar -Value 'MyVarValue'\r\n\r\n$StringToExpand = '$MyVar'\r\nWrite-Host \"ExpandStringIssue returns:\"\r\nExpandStringIssue -Value $StringToExpand\r\nWrite-Host \"-\"\r\nWrite-Host \"ExpandString returns:\"\r\n$ExecutionContext.InvokeCommand.ExpandString( $StringToExpand )\r\nWrite-Host \"-\"\r\n```\r\n\r\nWhen invoked, the module ExpandStringIssue function returns an empty line for the $MyVar expansion, and a line containing the actual value of $MyVar to demonstrate that the module actually is able to query $MyVar, just not through ExpandString.\r\nThe bottom ExpandString output was just added to show the actual output of ExpandString.\r\n"},"comment":{"url":"https://api.github.com/repos/PowerShell/PowerShell/issues/comments/427481070","html_url":"https://github.com/PowerShell/PowerShell/issues/7944#issuecomment-427481070","issue_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/7944","id":427481070,"node_id":"MDEyOklzc3VlQ29tbWVudDQyNzQ4MTA3MA==","user":{"login":"mklement0","id":588825,"node_id":"MDQ6VXNlcjU4ODgyNQ==","avatar_url":"https://avatars2.githubusercontent.com/u/588825?v=4","gravatar_id":"","url":"https://api.github.com/users/mklement0","html_url":"https://github.com/mklement0","followers_url":"https://api.github.com/users/mklement0/followers","following_url":"https://api.github.com/users/mklement0/following{/other_user}","gists_url":"https://api.github.com/users/mklement0/gists{/gist_id}","starred_url":"https://api.github.com/users/mklement0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mklement0/subscriptions","organizations_url":"https://api.github.com/users/mklement0/orgs","repos_url":"https://api.github.com/users/mklement0/repos","events_url":"https://api.github.com/users/mklement0/events{/privacy}","received_events_url":"https://api.github.com/users/mklement0/received_events","type":"User","site_admin":false},"created_at":"2018-10-05T19:55:12Z","updated_at":"2018-10-05T19:55:12Z","author_association":"CONTRIBUTOR","body":"Great stuff, @PetSerAl, thanks for sharing.\r\n\r\nIf I understand this correctly, the `[scriptblock]::Create()` call could be simplified to simply output a regular expandable string, as its expansion will be delayed anyway:\r\n\r\n```powershell\r\n# Using a string argument, which is what [scriptblock]::Create() requires anyway\r\n[scriptblock]::Create(' \"`\"$a`\"\" ') \r\n```\r\n\r\nor, perhaps more typically, if embedded quoting isn't needed:\r\n\r\n```\r\n[scriptblock]::Create(' \"The answer is $a.\" ') \r\n```\r\n\r\n"}}
{"actor":{"display_login":"mklement0"}}