Hosted PS core in ASP.Net core The system cannot find the file specified
See original GitHub issueASP.NET core 2.0 app.
.csproj:
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="6.0.0-beta.8" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.0-beta.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.WSMan.Management" Version="6.0.0-beta.8" />
controller code:
using (PowerShell ps = PowerShell.Create())
{
var result = ps.AddScript(@"New-Item -Path 'C:\Programming\' -Type Directory -Name TestFolder").Invoke();
}
Results in an exeption:
System.Management.Automation.RuntimeException: The following errors occurred when updating the assembly list for the runspace: Could not load file or assembly ‘C:\Programming\Intranet\Intranet\Microsoft.PowerShell.Commands’. The system cannot find the file specified. Could not load file or assembly ‘C:\Programming\Intranet\Intranet\Microsoft.PowerShell’. The system cannot find the file specified. Could not load file or assembly ‘C:\Programming\Intranet\Intranet\Microsoft.PowerShell.Commands’. The system cannot find the file specified. Could not load file or assembly ‘C:\Programming\Intranet\Intranet\Microsoft.PowerShell.Commands’. The system cannot find the file specified. Could not load file or assembly ‘C:\Programming\Intranet\Intranet\Microsoft.PowerShell’. The system cannot find the file specified. Could not load file or assembly ‘C:\Programming\Intranet\Intranet\Microsoft.WSMan’. The system cannot find the file specified…
at System.Management.Automation.ExecutionContext.UpdateAssemblyCache()
at System.Management.Automation.Runspaces.RunspaceConfigurationEntryCollection`1.Update(Boolean force)
at System.Management.Automation.Runspaces.RunspaceConfiguration.Bind(ExecutionContext executionContext)
at System.Management.Automation.AutomationEngine…ctor(PSHost hostInterface, RunspaceConfiguration runspaceConfiguration, InitialSessionState iss)
at System.Management.Automation.Runspaces.LocalRunspace.DoOpenHelper()
at System.Management.Automation.Runspaces.LocalRunspace.OpenHelper(Boolean syncCall)
at System.Management.Automation.Runspaces.RunspaceBase.CoreOpen(Boolean syncCall)
at System.Management.Automation.Runspaces.RunspaceBase.Open()
at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection
1 input, PSDataCollection
1 output, PSInvocationSettings settings)at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection
1 input, PSDataCollection
1 output, PSInvocationSettings settings)at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke()
at Intranet.Controllers.PowershellController.Test() in C:\Programming\Intranet\Intranet\Controllers\PowershellController.cs:line 30
Issue Analytics
- State:
- Created 6 years ago
- Comments:19 (13 by maintainers)
I have verified that this bug is not related to #4196 and therefore not fixed by #4942 #4196. Although the behavior is very similar, but regardless the change of #4196, it repros anyway. I can take a look.
After talked with dongbo and verified the issue with further steps, the fix actually works.
The keypoint is building nuget package with the code change as local package and reference to the local package instead of going online (of course, the code change won’t affect the online package, beta.8 in this case).
Here is a repro step:
Generate the nuget package locally with the code change: $VersionSuffix = (“v6.0.0-beta.9” -split ‘-’)[-1] Publish-NuGetFeed -VersionSuffix $VersionSuffix
edit the nuget config file include the local package: go to …\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3 add following config to NuGet.config <add key="local-feed" value="C:\Github\powershellfull\p2\PowerShell\nuget-artifacts" /> where C:\Github\powershellfull\p2\PowerShell\nuget-artifacts is folder where previous neget package generates.
go to …\sample-dotnet2.0-powershell.beta.3\MyApp dotnet restore dotnet run
The exception is gone.