I’m running an ASP.NET Core 6 application on an IIS as a Rest Api calling Powershell scripts for specific tasks. It works well from my laptop (Windows 10) but doesn’t work when I’m running it on a Windows Server 2019 Version 1809 Build 17763.1935. The error tells me that it cannnot find the assembly "Microsoft.Management.Infrastructure".
Unhandled exception. System.IO.FileNotFoundException: Could not load
file or assembly ‘Microsoft.Management.Infrastructure,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’.
Das System kann die angegebene Datei nicht finden. File name:
‘Microsoft.Management.Infrastructure, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35’
"Das System kann die angegebene Datei nicht finden." = "File not found."
Did anyone encounter that problem too? The server has the following things installed:
- Microsoft .NET 6.0.3 – Windows Server Hosting Microsoft .NET Runtime
- 6.0.3 (x64) Microsoft .NET Runtime – 6.0.3 (x86)
- Microsoft .NET SDK 6.0.201 (x64) Microsoft
- ASP.NET Core 6.0.3 – Shared Framework (x64)
- Microsoft ASP.NET Core 6.0.3 – Shared Framework (x86)
- Microsoft Visual C++ 2015-2019 Redistributable (x64) – 14.28.29913
- Microsoft Visual C++ 2015-2019 Redistributable (x86) – 14.28.29913
- IIS 10.0
- Windows PowerShell 5.1
- PowerShell 7.2.1
Now to test if it is the server setup missing something I wrote a little .net console application with this code
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Microsoft.PowerShell;
var initialSessionState = InitialSessionState.CreateDefault();
initialSessionState.ExecutionPolicy = ExecutionPolicy.Unrestricted;
using (PowerShell powerShell = PowerShell.Create(initialSessionState))
{
powerShell.AddCommand("whoami");
foreach (var item in powerShell.Invoke())
{
Console.WriteLine(item.BaseObject.ToString());
}
if (powerShell.HadErrors)
{
throw new Exception("powershell script had errors");
}
}
I can run this program on the server without problems. But if I copy-paste this exact code into my Api code it fails with the above error. Any ideas?
EDIT 1: The error does also occur when i run the .exe directly form the command line instead of starting the IIS instance.
EDIT 2: Every DLL that is in the bindebug folder (the one i use for testing on my laptop and that runs fine) is also contained in the binrelease folder (the one that gets published to the IIS). There are DLL that are in the release folder but not in the debug folder:
- Microsoft.Management.Infrastructure.CimCmdlets.dll
- Microsoft.PowerShell.Commands.Diagnostics.dll
- Microsoft.PowerShell.Commands.Management.dll
- Microsoft.PowerShell.Commands.Utility.dll
- Microsoft.PowerShell.ConsoleHost.dll
- Microsoft.PowerShell.CoreCLR.Eventing.dll
- Microsoft.PowerShell.SDK.dll Microsoft.PowerShell.Security.dll
- Microsoft.WSMan.Management.dll Microsoft.WSMan.Runtime.dll
- PowerShell.Core.Instrumentation.dll pwrshplugin.dll sni.dll
- System.Management.Automation.dll
The file "Microsoft.Management.Infrastructure.dll" is neither in the release nor the debug folder.
The projects csproj-file looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<!-- https://github.com/nhibernate/nhibernate-core/issues/2603 -->
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.2.1" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="System.DirectoryServices" Version="6.0.0" />
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="6.0.0" />
</ItemGroup>
</Project>
EDIT 3:
Extending the csproj-file by
<PackageReference Include="Microsoft.Management.Infrastructure" Version="2.0.0" />
<PackageReference Include="Microsoft.Management.Infrastructure.CimCmdlets" Version="7.2.2" />
<PackageReference Include="Microsoft.Management.Infrastructure.Runtime.Win" Version="2.0.0" />
does not work either. Also referencing "Microsoft.Management.Infrastructure" Version 1.0.0 instead of 2.0.0 does not work because "System.Management.Automation" seems to require Version 2.0.0 of that package.
2
Answers
I resolved the issue by building/publishing the application on the target server. Nothing changed in the project or the code. The IIS stayed the same too. It just works now after building it locally on the server.
I found that the targeted runtime is important. This will not work for a generic
win-x64
runtime, butwin10-x64
does.Both this flag in the csproj:
and these switches in the publish command:
I’ve made a small project using the above in a PowerShell API as Windows Service on GitHub
These are the most recent references I used to get powershell to work: