Title: Pass parameter by reference to a cmdlet written in C# · Issue #14384 · PowerShell/PowerShell · GitHub
Open Graph Title: Pass parameter by reference to a cmdlet written in C# · Issue #14384 · PowerShell/PowerShell
X Title: Pass parameter by reference to a cmdlet written in C# · Issue #14384 · PowerShell/PowerShell
Description: I am trying to write a simple PowerShell cmdlet in C# that accepts an instance of ArrayList and adds an item to it. The issue I've encountered is that cmdlet receives a copy of the collection so modifying it inside the cmdlet does not wo...
Open Graph Description: I am trying to write a simple PowerShell cmdlet in C# that accepts an instance of ArrayList and adds an item to it. The issue I've encountered is that cmdlet receives a copy of the collection so mo...
X Description: I am trying to write a simple PowerShell cmdlet in C# that accepts an instance of ArrayList and adds an item to it. The issue I've encountered is that cmdlet receives a copy of the collection s...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/14384
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Pass parameter by reference to a cmdlet written in C# ","articleBody":"I am trying to write a simple PowerShell cmdlet in C# that accepts an instance of ArrayList and adds an item to it. The issue I've encountered is that cmdlet receives a copy of the collection so modifying it inside the cmdlet does not work. I assume that this is an intended behavior, but cannot find any proof for that in the documentation. I looked specifically here: https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/cmdlet-overview?view=powershell-7.1, but also read other sections. Below is a program to reproduce the issue. Can anyone point to the documentation or explain why cmdlet parameter binding works this way please?\r\n\r\n## Steps to reproduce\r\n\r\n```\r\nusing System;\r\nusing System.Collections;\r\nusing System.Collections.ObjectModel;\r\nusing System.Management.Automation;\r\nusing System.Management.Automation.Runspaces;\r\n\r\nnamespace CustomPowerShellCmdlet\r\n{\r\n class Program\r\n {\r\n static void Main(string[] args)\r\n {\r\n string powerShellScript = @\"\r\nWrite-Output ('PowerShell Version is ' + $PSVersionTable.PSVersion)\r\n\r\n$A = New-Object System.Collections.ArrayList\r\n$A.Add('3') | Out-Null\r\nWrite-Output ('Count is ' + $A.Count)\r\n\r\n$B = New-Object System.Collections.ArrayList\r\nAdd-CollectionItem -Collection $B -Item '1'\r\nAdd-CollectionItem -Collection $B -Item '2'\r\nforeach ($Item in $B) {\r\n Write-Output $Item\r\n}\r\nWrite-Output ('Count is ' + $B.Count)\r\n\";\r\n\r\n var output = RunPowerShellScript(powerShellScript);\r\n\r\n foreach (PSObject psObject in output)\r\n {\r\n Console.WriteLine(psObject.BaseObject.ToString());\r\n }\r\n }\r\n\r\n private static Collection\u003cPSObject\u003e RunPowerShellScript(string powerShellScript)\r\n {\r\n InitialSessionState initialSessionState = InitialSessionState.CreateDefault();\r\n initialSessionState.Commands.Add(new SessionStateCmdletEntry(\"Add-CollectionItem\", typeof(AddCollectionItemCmdlet), null));\r\n\r\n using (var runspace = RunspaceFactory.CreateRunspace(initialSessionState))\r\n {\r\n runspace.Open();\r\n\r\n using (var ps = PowerShell.Create())\r\n {\r\n ps.Runspace = runspace;\r\n\r\n ps.AddScript(powerShellScript);\r\n\r\n\r\n return ps.Invoke();\r\n }\r\n }\r\n }\r\n\r\n [Cmdlet(VerbsCommon.Add, \"CollectionItem\")]\r\n public class AddCollectionItemCmdlet : Cmdlet\r\n {\r\n [Parameter(Mandatory = true)]\r\n [AllowEmptyCollection]\r\n public ArrayList Collection { get; set; }\r\n\r\n [Parameter(Mandatory = true)]\r\n public object Item { get; set; }\r\n\r\n protected override void BeginProcessing()\r\n {\r\n Collection.Add(Item);\r\n }\r\n }\r\n }\r\n}\r\n\r\n```\r\n\r\n## Expected behavior\r\n\r\n```Collection parameter is passed by reference\r\n```\r\n\r\n## Actual behavior\r\n\r\n```a copy of ArrayList is passed to cmdlet.\r\n\r\n```\r\n\r\n## Environment data\r\n\r\nPSVersion 7.1.0\r\nPSEdition Core\r\nGitCommitId 7.1.0\r\nOS Microsoft Windows 10.0.19041\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n\r\n```none\r\n\r\n```\r\n","author":{"url":"https://github.com/AlexKichkailo","@type":"Person","name":"AlexKichkailo"},"datePublished":"2020-12-10T21:40:23.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/14384/PowerShell/issues/14384"}
| 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:d52291e9-d27b-c13e-5baa-da32fdf0e65c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 818A:A7CE4:101FB0F:159C005:6A5B7001 |
| html-safe-nonce | 9d428b1c56500bf28771f714bbe1d9dab4bdbb949ed545636d2719acf2504c87 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MThBOkE3Q0U0OjEwMUZCMEY6MTU5QzAwNTo2QTVCNzAwMSIsInZpc2l0b3JfaWQiOiI1Mzk2NzM0NTY5MzY5NzkyNTEzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d051abe0bda67622191a0dbb6c7c7c85cbbb0766889acf1f614445dd1f0be5c6 |
| hovercard-subject-tag | issue:761638797 |
| 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/14384/issue_layout |
| twitter:image | https://opengraph.githubassets.com/38e4031b257440e414404fd3e2320638907dc0b1b08e8740f73757e5905554cd/PowerShell/PowerShell/issues/14384 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/38e4031b257440e414404fd3e2320638907dc0b1b08e8740f73757e5905554cd/PowerShell/PowerShell/issues/14384 |
| og:image:alt | I am trying to write a simple PowerShell cmdlet in C# that accepts an instance of ArrayList and adds an item to it. The issue I've encountered is that cmdlet receives a copy of the collection so mo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AlexKichkailo |
| 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