MegaBites-AI/Windows-powershell
0372
1# Host PowerShell Core in .NET Core Applications2 3## PowerShell Core v6.0.1 and Later4 5The runtime assemblies for Windows, Linux and OSX are now published in NuGet package version 6.*.6 7Please see the [.NET Core Sample Application](#net-core-sample-application) section for an example that uses PowerShell Core NuGet packages.8 9## .NET Core Sample Application10 11Note: The .NET Core `2.1` runtime and .NET Core SDK `2.1` or higher is required for the examples below:12 13- [sample](./sample)14 15You can find the sample application project `MyApp` in each of the above 2 sample folders. You can quickly test-run it using `dotnet run`.16To build the sample project properly for distribution, run the following command (make sure the required .NET Core SDK is in use):17 18```powershell19dotnet publish .\MyApp --configuration release20```21 22This builds it for the runtimes specified by the `RuntimeIdentifiers` property in the `.csproj` file.23Then you can run the `MyApp` binary from the publish folder and see the results:24 25```none26PS:> .\MyApp.exe27 28Evaluating 'Get-Command Write-Output' in PS Core Runspace29 30Write-Output31 32Evaluating '([S.M.A.ActionPreference], [S.M.A.AliasAttribute]).FullName' in PS Core Runspace33 34System.Management.Automation.ActionPreference35System.Management.Automation.AliasAttribute36```37 38## Special Hosting Scenario For Native Host39 40There is a special hosting scenario for native hosts,41where Trusted Platform Assemblies (TPA) do not include PowerShell assemblies,42such as the in-box `powershell.exe` in Nano Server and the Azure DSC host.43 44For such hosting scenarios, the native host needs to bootstrap by calling [`PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext`](https://learn.microsoft.com/dotnet/api/system.management.automation.powershellassemblyloadcontextinitializer.setpowershellassemblyloadcontext).45When using this API, the native host can pass in the path to the directory that contains PowerShell assemblies.46A handler will then be registered to the [`Resolving`](https://github.com/dotnet/corefx/blob/d6678e9653defe3cdfff26b2ff62135b6b22c77f/src/System.Runtime.Loader/ref/System.Runtime.Loader.cs#L38)47event of the default load context to deal with the loading of assemblies from that directory.48 