Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. running Powershell from ASP.NET Core 6

running Powershell from ASP.NET Core 6

Scheduled Pinned Locked Moved .NET (Core and Framework)
windows-adminc++asp-netdotnetsysadmin
2 Posts 2 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johannes B Latzel
    wrote on last edited by
    #1

    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?

    D 1 Reply Last reply
    0
    • J Johannes B Latzel

      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?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups