简体   繁体   English

从 ASP.NET Core 6 运行 Powershell

[英]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.我在 IIS 上运行 ASP.NET Core 6 应用程序作为 Rest Api 为特定任务调用 Powershell 脚本。 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.它在我的笔记本电脑 (Windows 10) 上运行良好,但在 Windows Server 2019 Version 1809 Build 17763.1935 上运行时无法运行。 The error tells me that it cannnot find the assembly "Microsoft.Management.Infrastructure".该错误告诉我它找不到程序集“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'. System.IO.FileNotFoundException:无法加载文件或程序集“Microsoft.Management.Infrastructure,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”。 Das System kann die angegebene Datei nicht finden. Das System kann die angegebene Datei nicht finden。 File name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'文件名:'Microsoft.Management.Infrastructure,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35'

"Das System kann die angegebene Datei nicht finden." “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 Microsoft .NET 6.0.3 - Windows 服务器托管 Microsoft .NET 运行时
  • 6.0.3 (x64) Microsoft .NET Runtime - 6.0.3 (x86) 6.0.3 (x64) Microsoft .NET 运行时 - 6.0.3 (x86)
  • Microsoft .NET SDK 6.0.201 (x64) Microsoft微软 .NET SDK 6.0.201 (x64) 微软
  • ASP.NET Core 6.0.3 - Shared Framework (x64) ASP.NET 核心 6.0.3 - 共享框架 (x64)
  • Microsoft ASP.NET Core 6.0.3 - Shared Framework (x86) Microsoft ASP.NET 核心 6.0.3 - 共享框架 (x86)
  • Microsoft Visual C++ 2015-2019 Redistributable (x64) - 14.28.29913 Microsoft Visual C++ 2015-2019 可再发行 (x64) - 14.28.29913
  • Microsoft Visual C++ 2015-2019 Redistributable (x86) - 14.28.29913 Microsoft Visual C++ 2015-2019 可再发行 (x86) - 14.28.29913
  • IIS 10.0 IIS 10.0
  • Windows PowerShell 5.1 Windows PowerShell 5.1
  • PowerShell 7.2.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现在测试它是否是服务器设置丢失的东西我用这段代码写了一个小的 .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");
    }
}

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.但是,如果我将这段确切的代码复制粘贴到我的 Api 代码中,它会因上述错误而失败。 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.编辑 1 :当我直接从命令行运行 the.exe 而不是启动 IIS 实例时,也会发生错误。

EDIT 2 : Every DLL that is in the bin\debug folder (the one i use for testing on my laptop and that runs fine) is also contained in the bin\release folder (the one that gets published to the IIS).编辑 2 :bin\debug 文件夹中的每个 DLL(我在笔记本电脑上用于测试并且运行良好的文件夹)也包含在 bin\release 文件夹中(发布到 IIS 的文件夹)。 There are DLL that are in the release folder but not in the debug folder:有DLL在release文件夹中,但不在debug文件夹中:

  • Microsoft.Management.Infrastructure.CimCmdlets.dll Microsoft.Management.Infrastructure.CimCmdlets.dll
  • Microsoft.PowerShell.Commands.Diagnostics.dll Microsoft.PowerShell.Commands.Diagnostics.dll
  • Microsoft.PowerShell.Commands.Management.dll Microsoft.PowerShell.Commands.Management.dll
  • Microsoft.PowerShell.Commands.Utility.dll Microsoft.PowerShell.Commands.Utility.dll
  • Microsoft.PowerShell.ConsoleHost.dll Microsoft.PowerShell.ConsoleHost.dll
  • Microsoft.PowerShell.CoreCLR.Eventing.dll Microsoft.PowerShell.CoreCLR.Eventing.dll
  • Microsoft.PowerShell.SDK.dll Microsoft.PowerShell.Security.dll Microsoft.PowerShell.SDK.dll Microsoft.PowerShell.Security.dll
  • Microsoft.WSMan.Management.dll Microsoft.WSMan.Runtime.dll Microsoft.WSMan.Management.dll Microsoft.WSMan.Runtime.dll
  • PowerShell.Core.Instrumentation.dll pwrshplugin.dll sni.dll PowerShell.Core.Instrumentation.dll pwrshplugin.dll sni.dll
  • System.Management.Automation.dll系统.管理.自动化.dll

The file "Microsoft.Management.Infrastructure.dll" is neither in the release nor the debug folder.文件“Microsoft.Management.Infrastructure.dll”既不在发行版也不在调试文件夹中。

The projects csproj-file looks like this:项目 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>

EDIT 3 : Extending the csproj-file by编辑 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" />

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.同样引用“Microsoft.Management.Infrastructure”1.0.0 版而不是 2.0.0 版也不起作用,因为“System.Management.Automation”似乎需要 package 的 2.0.0 版。

I found that the targeted runtime is important.我发现目标运行时很重要。 This will not work for a generic win-x64 runtime, but win10-x64 does.这不适用于通用的win-x64运行时,但win10-x64可以。

Both this flag in the csproj: csproj 中的这两个标志:

<RuntimeIdentifier>win10-x64</RuntimeIdentifier>

and these switches in the publish command:以及发布命令中的这些开关:

dotnet publish --sc --runtime win10-x64

I've made a small project using the above in a PowerShell API as Windows Service on GitHub我在PowerShell API 作为 Windows Service on GitHub 做了一个小项目

These are the most recent references I used to get powershell to work:这些是我用来使 powershell 正常工作的最新参考资料:

<PackageReference Include="Microsoft.Management.Infrastructure.CimCmdlets" Version="7.2.6" />
<PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="7.2.6" />
<PackageReference Include="Microsoft.PowerShell.Commands.Management" Version="7.2.6" />
<PackageReference Include="Microsoft.PowerShell.Commands.Utility" Version="7.2.6" />
<PackageReference Include="Microsoft.PowerShell.ConsoleHost" Version="7.2.6" />
<PackageReference Include="Microsoft.PowerShell.CoreCLR.Eventing" Version="7.2.6" />
<PackageReference Include="Microsoft.PowerShell.Native" Version="7.2.0" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.6" />
<PackageReference Include="Microsoft.PowerShell.Security" Version="7.2.6" />
<PackageReference Include="Microsoft.WSMan.Management" Version="7.2.6" />
<PackageReference Include="Microsoft.WSMan.Runtime" Version="7.2.6" />
<PackageReference Include="System.Management.Automation" Version="7.2.6" />

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. IIS 也保持不变。 It just works now after building it locally on the server.在服务器上本地构建后,它现在就可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM