running Powershell from ASP.NET Core 6
-
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?
-
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?
Chances are the account your ASP.NET site is running under does not have permissions to run Powershell. Try creating a normal user account and running your site under that account to test. No, that is not a viable solution if it does work. The entire point of having such a restricted account is to prevent security issues if someone gets your code to execute something malicious.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak