运行 来自 ASP.NET Core 6 的 Powershell

running Powershell from ASP.NET Core 6

我正在 运行在 IIS 上安装一个 ASP.NET Core 6 应用程序作为 Rest Api 为特定任务调用 Powershell 脚本。它在我的笔记本电脑 (Windows 10) 上运行良好,但当我 运行 在 Windows Server 2019 Version 1809 Build 17763.1935 上安装它时它不起作用。该错误告诉我它找不到程序集“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。” =“找不到文件。”

有没有人也遇到过这个问题?服务器安装了以下东西:

现在要测试它是否是服务器设置丢失的东西,我用这段代码编写了一个小的 .net 控制台应用程序

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");
    }
}

我可以 运行 这个程序在服务器上没有问题。但是,如果我将这段确切的代码复制粘贴到我的 Api 代码中,它会因上述错误而失败。有什么想法吗?

编辑 1:当我 运行 .exe 直接形成命令行而不是启动 IIS 实例时,也会发生错误。

编辑 2:bin\debug 文件夹中的每个 DLL(我用来在笔记本电脑上测试的那个 运行 没问题)也包含在 bin\release 文件夹(发布到 IIS 的文件夹)中。发布文件夹中有但调试文件夹中没有的DLL:

文件“Microsoft.Management.Infrastructure.dll”既不在发布文件夹也不在调试文件夹中。

项目 csproj 文件如下所示:

<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>

编辑 3: 通过

扩展 csproj 文件
<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" />

也不行。同样引用“Microsoft.Management.Infrastructure”版本 1.0.0 而不是 2.0.0 也不起作用,因为“System.Management.Automation”似乎需要该包的版本 2.0.0。

我通过 building/publishing 目标服务器上的应用程序解决了这个问题。项目或代码没有任何变化。 IIS 也保持不变。在服务器上本地构建后,它现在可以正常工作了。