Title: Way to invoke a scriptblock with named parameters from C# · Issue #15004 · PowerShell/PowerShell · GitHub
Open Graph Title: Way to invoke a scriptblock with named parameters from C# · Issue #15004 · PowerShell/PowerShell
X Title: Way to invoke a scriptblock with named parameters from C# · Issue #15004 · PowerShell/PowerShell
Description: Summary of the new feature/enhancement Say I have a scriptblock like this: { param( $a, $b ) "a: $a" "b: $b" } And I want to invoke it from C#: var sb = ScriptBlock.Create("..."); var parameters = new Hashtable { { "a", "A" }, { "b", "B"...
Open Graph Description: Summary of the new feature/enhancement Say I have a scriptblock like this: { param( $a, $b ) "a: $a" "b: $b" } And I want to invoke it from C#: var sb = ScriptBlock.Create("..."); var parameters = ...
X Description: Summary of the new feature/enhancement Say I have a scriptblock like this: { param( $a, $b ) "a: $a" "b: $b" } And I want to invoke it from C#: var sb = ScriptBlock.Create("...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/15004
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Way to invoke a scriptblock with named parameters from C#","articleBody":"## Summary of the new feature/enhancement\r\n\r\nSay I have a scriptblock like this:\r\n\r\n```powershell\r\n{\r\n param(\r\n $a,\r\n $b\r\n )\r\n\r\n \"a: $a\"\r\n \"b: $b\"\r\n}\r\n```\r\n\r\nAnd I want to invoke it from C#:\r\n\r\n```csharp\r\nvar sb = ScriptBlock.Create(\"...\");\r\nvar parameters = new Hashtable\r\n{\r\n { \"a\", \"A\" },\r\n { \"b\", \"B\" },\r\n};\r\n\r\nsb.Invoke(?);\r\nInvokeCommand.InvokeScript(sb, ?);\r\n\r\nusing (var pwsh = PowerShell.Create())\r\n{\r\n pwsh.\u003c?>.AddParameters(parameters).Invoke();\r\n}\r\n```\r\n\r\nIs there a way to do this currently in PowerShell, and if not can we have one?\r\n\r\nThe best way I've found to do this so far is to use reflection to create a `FunctionInfo` object around the scriptblock for invocation:\r\n\r\n```csharp\r\nvar sb = ScriptBlock.Create(\"param($a, $b) \\\"a: $a\\\",\\\"b: $b\\\"\");\r\nvar runspace = RunspaceFactory.CreateRunspace();\r\nrunspace.Open();\r\n\r\nPropertyInfo runspaceExecutionContextProperty = typeof(Runspace).GetProperty(\"ExecutionContext\", BindingFlags.Instance | BindingFlags.NonPublic);\r\nType ecType = runspaceExecutionContextProperty.PropertyType;\r\n\r\nConstructorInfo funcInfoCtor = typeof(FunctionInfo).GetConstructor(\r\n BindingFlags.NonPublic | BindingFlags.Instance,\r\n binder: null,\r\n new[] { typeof(string), typeof(ScriptBlock), ecType },\r\n modifiers: null);\r\n\r\nobject executionContext = runspaceExecutionContextProperty.GetValue(runspace);\r\nvar sbFuncInfo = (FunctionInfo)funcInfoCtor.Invoke(new object[] { string.Empty, sb, executionContext });\r\n\r\nusing (var pwsh = PowerShell.Create())\r\n{\r\n pwsh.AddCommand(sbFuncInfo).AddParameters(new Dictionary\u003cstring, object>{ {\"a\", \"A\"}, {\"b\", \"B\"} });\r\n\r\n foreach (PSObject result in pwsh.Invoke())\r\n {\r\n Console.WriteLine(result);\r\n }\r\n}\r\n```\r\n\r\nBut this is very involved, messy, requires overhead and isn't public/officially supported.\r\n\r\n## Proposed technical implementation details (optional)\r\n\r\nSome considerations:\r\n\r\n- While the `PowerShell` API is a good place for this, it doesn't currently have good support for scriptblocks (something which could be improved)\r\n- Other APIs do support scriptblocks well, such as `CommandInvocationIntrinsics.InvokeScript` and `ScriptBlock.Invoke[WithContext]` and it would be nice to extend those to enable named parameter passing since they allow simpler control of the execution context than the `PowerShell` API\r\n","author":{"url":"https://github.com/rjmholt","@type":"Person","name":"rjmholt"},"datePublished":"2021-03-12T01:51:38Z","url":"https://github.com/PowerShell/PowerShell/issues/15004"}
| 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:beaf79ac-ab93-149b-8784-9dbf9a451bdb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E926:1F8078:8EC44D:C85AEE:6AFDC7A7 |
| html-safe-nonce | f388b376267cdf591e2be6d92ff0f52473d8a6df74c59e4f88736299a6d062dc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOTI2OjFGODA3ODo4RUM0NEQ6Qzg1QUVFOjZBRkRDN0E3IiwidmlzaXRvcl9pZCI6IjIyNTc4MzQ2ODI2MTc4MDg4MDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | dd8240b64407ddbdd90923fbfc335f8cdf5e83b9fcde4c61f464791af10d5769 |
| hovercard-subject-tag | issue:829679626 |
| 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/15004/issue_layout |
| twitter:image | https://opengraph.githubassets.com/639f94c63faf7467b04a336477d656e6da70728cf1e652a15de285bb6aa8d70a/PowerShell/PowerShell/issues/15004 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/639f94c63faf7467b04a336477d656e6da70728cf1e652a15de285bb6aa8d70a/PowerShell/PowerShell/issues/15004 |
| og:image:alt | Summary of the new feature/enhancement Say I have a scriptblock like this: { param( $a, $b ) "a: $a" "b: $b" } And I want to invoke it from C#: var sb = ScriptBlock.Create("..."); var parameters = ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rjmholt |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9ab00cc3b4fa90bbd3da70765dd38efc23cfa9d9b0930e41feea2bf67f60ecaf |
| 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 | 1b795ba060b92691fc16c5a12cf2bf95e83432b4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width