René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:194b4a36-3eef-d40f-910b-7b9ef8d9620b
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB382:1E5037:1C3A89:283C5C:6AFD520A
html-safe-nonce89e123062b14f8bc3b613ec99e02f1f0a22f883ba86fd8024924c2126dbbe818
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzgyOjFFNTAzNzoxQzNBODk6MjgzQzVDOjZBRkQ1MjBBIiwidmlzaXRvcl9pZCI6IjY3ODAwMjE5MDYyNDMxNDYyNTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac03792462a60942139048c2e747ca3221b32c06485351c972e8df8c8bb00ef47b
hovercard-subject-tagissue:653055005
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/13132/issue_layout
twitter:imagehttps://opengraph.githubassets.com/76760ab684756b2471a62f80e8d32d4f8ba7a438c1ff4d105fef2e58bf6ae6a9/PowerShell/PowerShell/issues/13132
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/76760ab684756b2471a62f80e8d32d4f8ba7a438c1ff4d105fef2e58bf6ae6a9/PowerShell/PowerShell/issues/13132
og:image:altWe 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamenormj
hostnamegithub.com
expected-hostnamegithub.com
None66653c2e55a6fff2930be1bdf07ee4f8aa137e3956d056c822563be6f878755e
turbo-cache-controlno-preview
go-importgithub.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git
octolytics-dimension-user_id11524380
octolytics-dimension-user_loginPowerShell
octolytics-dimension-repository_id49609581
octolytics-dimension-repository_nwoPowerShell/PowerShell
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id49609581
octolytics-dimension-repository_network_root_nwoPowerShell/PowerShell
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseb6f01f7cb9f35f8c09949773241a3d49dd04b340
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/PowerShell/PowerShell/issues/13132#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Fissues%2F13132
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Fissues%2F13132
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=PowerShell%2FPowerShell
Reloadhttps://github.com/PowerShell/PowerShell/issues/13132
Reloadhttps://github.com/PowerShell/PowerShell/issues/13132
Reloadhttps://github.com/PowerShell/PowerShell/issues/13132
PowerShellhttps://github.com/PowerShell
PowerShellhttps://github.com/PowerShell/PowerShell
Codehttps://github.com/PowerShell/PowerShell
Issues1.3k (1.3k)https://github.com/PowerShell/PowerShell/issues
Pull requests365 (365)https://github.com/PowerShell/PowerShell/pulls
Discussionshttps://github.com/PowerShell/PowerShell/discussions
Actionshttps://github.com/PowerShell/PowerShell/actions
Projectshttps://github.com/PowerShell/PowerShell/projects
Security and quality3 (3)https://github.com/PowerShell/PowerShell/security
Insightshttps://github.com/PowerShell/PowerShell/insights
https://github.com/PowerShell/PowerShell
New issuehttps://github.com/login?return_to=https://github.com/PowerShell/PowerShell/issues/13132
New issuehttps://github.com/login?return_to=https://github.com/PowerShell/PowerShell/issues/13132
Scripts using -Parallel fail when run using Microsoft.PowerShell.SDK and a publish runtime flaghttps://github.com/PowerShell/PowerShell/issues/13132#top
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get ahttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Question%22
Resolution-AnsweredThe question is answered.https://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Resolution-Answered%22
WG-Cmdletsgeneral cmdlet issueshttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-Cmdlets%22
WG-DevEx-SDKhosting PowerShell as a runtime, PowerShell's APIs, PowerShell Standard, or development of moduleshttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-DevEx-SDK%22
https://github.com/normj
Norm Johanson (normj)https://github.com/normj
on Jul 8, 2020https://github.com/PowerShell/PowerShell/issues/13132#issue-653055005
Norm Johanson (normj)https://github.com/normj
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get ahttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Question%22
on Jul 8, 2020https://github.com/PowerShell/PowerShell/issues/13132#event-3523814043
Ilya (iSazonov)https://github.com/iSazonov
Area-Cmdlets-Corehttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Area-Cmdlets-Core%22
WG-DevEx-SDKhosting PowerShell as a runtime, PowerShell's APIs, PowerShell Standard, or development of moduleshttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-DevEx-SDK%22
on Jul 8, 2020https://github.com/PowerShell/PowerShell/issues/13132#event-3524419109
https://github.com/iSazonov
Ilya (iSazonov)https://github.com/iSazonov
on Jul 8, 2020https://github.com/PowerShell/PowerShell/issues/13132#issuecomment-655418357
Paul Higinbotham (@PaulHigin)https://github.com/PaulHigin
https://github.com/PaulHigin
Paul Higinbotham (PaulHigin)https://github.com/PaulHigin
on Jul 9, 2020https://github.com/PowerShell/PowerShell/issues/13132#issuecomment-656232507
Dongbo Wang (@daxian-dbw)https://github.com/daxian-dbw
https://github.com/normj
Norm Johanson (normj)https://github.com/normj
on Jul 9, 2020https://github.com/PowerShell/PowerShell/issues/13132#issuecomment-656234592
https://github.com/daxian-dbw
Dongbo Wang (daxian-dbw)https://github.com/daxian-dbw
on Jul 9, 2020https://github.com/PowerShell/PowerShell/issues/13132#issuecomment-656238488
#11832 (comment)https://github.com/PowerShell/PowerShell/pull/11832#issuecomment-584882314
Aditya Patwardhan (@adityapatwardhan)https://github.com/adityapatwardhan
https://github.com/daxian-dbw
Dongbo Wang (daxian-dbw)https://github.com/daxian-dbw
on Jul 9, 2020https://github.com/PowerShell/PowerShell/issues/13132#issuecomment-656246670
herehttps://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs#L312
this codehttps://github.com/PowerShell/PowerShell/blob/5ad8a5916f37835feb0476396ff7c79e748c2577/src/System.Management.Automation/utils/PsUtils.cs#L145-L165
Norm Johanson (@normj)https://github.com/normj
https://github.com/adityapatwardhan
Aditya Patwardhan (adityapatwardhan)https://github.com/adityapatwardhan
on Jul 9, 2020https://github.com/PowerShell/PowerShell/issues/13132#issuecomment-656246836
https://github.com/daxian-dbw
Dongbo Wang (daxian-dbw)https://github.com/daxian-dbw
on Jul 9, 2020https://github.com/PowerShell/PowerShell/issues/13132#issuecomment-656248102
Aditya Patwardhan (@adityapatwardhan)https://github.com/adityapatwardhan
https://github.com/normj
Norm Johanson (normj)https://github.com/normj
on Jul 10, 2020https://github.com/PowerShell/PowerShell/issues/13132#issuecomment-656536023
Ilya (iSazonov)https://github.com/iSazonov
Resolution-AnsweredThe question is answered.https://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Resolution-Answered%22
on Jul 10, 2020https://github.com/PowerShell/PowerShell/issues/13132#event-3532809588
completedhttps://github.com/PowerShell/PowerShell/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Jul 12, 2020https://github.com/PowerShell/PowerShell/issues/13132#event-3536688969
Ryan Yates (kilasuit)https://github.com/kilasuit
WG-Cmdletsgeneral cmdlet issueshttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-Cmdlets%22
WG-Cmdlets-Corehttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-Cmdlets-Core%22
on Jul 17, 2026https://github.com/PowerShell/PowerShell/issues/13132#event-28143740294
Sign up for freehttps://github.com/signup?return_to=https://github.com/PowerShell/PowerShell/issues/13132
Sign in to commenthttps://github.com/login?return_to=https://github.com/PowerShell/PowerShell/issues/13132
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get ahttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Question%22
Resolution-AnsweredThe question is answered.https://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Resolution-Answered%22
WG-Cmdletsgeneral cmdlet issueshttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-Cmdlets%22
WG-DevEx-SDKhosting PowerShell as a runtime, PowerShell's APIs, PowerShell Standard, or development of moduleshttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-DevEx-SDK%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.