Title: Scripts using -Parallel fail when run using Microsoft.PowerShell.SDK and a publish runtime flag · Issue #13132 · PowerShell/PowerShell · GitHub
Open Graph Title: Scripts using -Parallel fail when run using Microsoft.PowerShell.SDK and a publish runtime flag · Issue #13132 · PowerShell/PowerShell
X Title: Scripts using -Parallel fail when run using Microsoft.PowerShell.SDK and a publish runtime flag · Issue #13132 · PowerShell/PowerShell
Description: We use the Microsoft.PowerShell.SDK (7.0.2) NuGet package in a .NET Core 3.1 application to execute PowerShell 7 scripts in a Linux environment. We have found when the .NET application is published with the runtime linux-x64 to reduce th...
Open Graph Description: We use the Microsoft.PowerShell.SDK (7.0.2) NuGet package in a .NET Core 3.1 application to execute PowerShell 7 scripts in a Linux environment. We have found when the .NET application is published...
X Description: We use the Microsoft.PowerShell.SDK (7.0.2) NuGet package in a .NET Core 3.1 application to execute PowerShell 7 scripts in a Linux environment. We have found when the .NET application is published...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/13132
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Scripts using -Parallel fail when run using Microsoft.PowerShell.SDK and a publish runtime flag","articleBody":"\r\n\r\nWe use the `Microsoft.PowerShell.SDK (7.0.2)` NuGet package in a .NET Core 3.1 application to execute PowerShell 7 scripts in a Linux environment. We have found when the .NET application is published with the runtime **linux-x64** to reduce the package size, scripts that use the `ForEach-Object -Parallel ...` fail with the following error message.\r\n```\r\n[Error] - The term 'Write-Host' is not recognized as the name of a cmdlet, function, script file, or operable program.\r\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\r\n```\r\nIf the .NET application is packaged without specifying runtime the script runs correctly. Also if the ForEach-Object command does not use the `-Parallel` parameter the script runs correctly.\r\n\r\n\r\n## Steps to reproduce\r\n\r\nHere is the test PowerShell script attempting to run through the .NET Application. \r\n```powershell\r\n$SharedVariable = \"Hello Shared Variable\"\r\n@(1..100) | ForEach-Object -Parallel { \r\n $i = $_\r\n Write-Host \"Running against: $i for SharedVariable: $($using:SharedVariable)\"\r\n}\r\n```\r\n\r\nHere is a trimmed down version of the .NET Core 3.1 application that is used for running the PowerShell script.\r\n\r\n```csharp\r\nusing System;\r\nusing System.IO;\r\nusing System.Management.Automation;\r\n\r\nnamespace PowerShellScriptRunner\r\n{\r\n class Program\r\n {\r\n static Exception _lastException;\r\n\r\n static void Main(string[] args)\r\n {\r\n if (args.Length == 0)\r\n {\r\n Console.Error.WriteLine(\"The first argument should the path to the script to run.\");\r\n System.Environment.Exit(-1);\r\n }\r\n else if (!File.Exists(args[0]))\r\n {\r\n Console.Error.WriteLine($\"Script {args[0]} can not be found.\");\r\n System.Environment.Exit(-2);\r\n }\r\n\r\n var executingScript = File.ReadAllText(args[0]);\r\n Console.WriteLine(\"Executing script:\");\r\n Console.WriteLine(executingScript);\r\n var ps = PowerShell.Create();\r\n SetupStreamHandlers(ps);\r\n\r\n ps.AddScript(executingScript);\r\n\r\n var output = new PSDataCollection\u003cPSObject>();\r\n var asyncResult = ps.BeginInvoke\u003cPSObject, PSObject>(null, output);\r\n\r\n while (!asyncResult.IsCompleted)\r\n {\r\n asyncResult.AsyncWaitHandle.WaitOne(500);\r\n }\r\n }\r\n\r\n private static void SetupStreamHandlers(PowerShell ps)\r\n {\r\n\r\n Func\u003cstring, EventHandler\u003cDataAddingEventArgs>> _loggerFactory = (prefix) =>\r\n {\r\n EventHandler\u003cDataAddingEventArgs> handler = (sender, e) =>\r\n {\r\n var message = e?.ItemAdded?.ToString();\r\n\r\n LogMessage(prefix, message);\r\n\r\n var errorRecord = e?.ItemAdded as ErrorRecord;\r\n if (errorRecord?.Exception != null)\r\n {\r\n _lastException = errorRecord.Exception;\r\n }\r\n };\r\n return handler;\r\n };\r\n\r\n ps.Streams.Verbose.DataAdding += _loggerFactory(\"Verbose\");\r\n ps.Streams.Information.DataAdding += _loggerFactory(\"Information\");\r\n ps.Streams.Warning.DataAdding += _loggerFactory(\"Warning\");\r\n ps.Streams.Error.DataAdding += _loggerFactory(\"Error\");\r\n }\r\n\r\n private static void LogMessage(string prefix, string message)\r\n {\r\n if (string.IsNullOrEmpty(message))\r\n {\r\n return;\r\n }\r\n\r\n if (!string.IsNullOrEmpty(prefix))\r\n {\r\n message = $\"[{prefix}] - {message}\";\r\n }\r\n\r\n Console.WriteLine(message);\r\n }\r\n }\r\n}\r\n```\r\n\r\nOn a Linux environment publish the project using the following command\r\n```\r\ndotnet publish --configuration Release --self-contained false --runtime linux-x64\r\n```\r\nthen execute the program\r\n```\r\ndotnet exec .\\bin\\Release\\netcoreapp3.1\\linux-x64\\publish\\PowerShellScriptRunner.dll \u003cpath-to-psscriptfile>\r\n```\r\n\r\nScript fails with the following error messages. \r\n```\r\n[Error] - The term 'Write-Host' is not recognized as the name of a cmdlet, function, script file, or operable program.\r\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\r\n```\r\nIf i repeat the steps above but remove the `--runtime linux-x64` switch there is no problem.\r\n\r\n\r\nOne theory I have is with the `--runtime linux-x64` switch the assemblies from the publish\\runtimes folder get flatten out at the root of the publish folder. But there is also the Modules in the publish\\runtimes\\unix\\lib\\netcoreapp3.1\\Modules and maybe something about the modules not being direct children of the assemblies anymore is causing the failure.\r\n\r\n\r\nOn a side note the value `--runtime win-x64` also doesn't seem to work either but for a different error. That error is \r\n```\r\nSystem.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.\r\nFile name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'\r\n at System.Reflection.RuntimeAssembly.GetExportedTypes()\r\n at System.Reflection.Assembly.get_ExportedTypes()\r\n at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)\r\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary`2& cmdlets, Dictionary`2& aliases, Dictionary`2& providers)\r\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary`2& cmdlets, Dictionary`2& aliases, Dictionary`2& providers, String& helpFile)\r\n at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)\r\n at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()\r\n at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)\r\n at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()\r\n at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)\r\n```\r\nWhich I can see the `Microsoft.Management.Infrastructure.dll` is missing in the publish folder but `Microsoft.Management.Infrastructure.CimCmdlets.dll` is there. This isn't my use case as I'm on Linux but maybe related somehow. \r\n\r\n\r\n## Expected behavior\r\n\r\nScripts using `-Parallel` argument execute successfully.\r\n","author":{"url":"https://github.com/normj","@type":"Person","name":"normj"},"datePublished":"2020-07-08T07:18:22Z","url":"https://github.com/PowerShell/PowerShell/issues/13132"}
| 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:194b4a36-3eef-d40f-910b-7b9ef8d9620b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B382:1E5037:1C3A89:283C5C:6AFD520A |
| html-safe-nonce | 89e123062b14f8bc3b613ec99e02f1f0a22f883ba86fd8024924c2126dbbe818 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzgyOjFFNTAzNzoxQzNBODk6MjgzQzVDOjZBRkQ1MjBBIiwidmlzaXRvcl9pZCI6IjY3ODAwMjE5MDYyNDMxNDYyNTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 03792462a60942139048c2e747ca3221b32c06485351c972e8df8c8bb00ef47b |
| hovercard-subject-tag | issue:653055005 |
| 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/13132/issue_layout |
| twitter:image | https://opengraph.githubassets.com/76760ab684756b2471a62f80e8d32d4f8ba7a438c1ff4d105fef2e58bf6ae6a9/PowerShell/PowerShell/issues/13132 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/76760ab684756b2471a62f80e8d32d4f8ba7a438c1ff4d105fef2e58bf6ae6a9/PowerShell/PowerShell/issues/13132 |
| og:image:alt | We use the Microsoft.PowerShell.SDK (7.0.2) NuGet package in a .NET Core 3.1 application to execute PowerShell 7 scripts in a Linux environment. We have found when the .NET application is published... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | normj |
| 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 | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width