首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从ASP.NET核心6运行Powershell

从ASP.NET核心6运行Powershell
EN

Stack Overflow用户
提问于 2022-03-18 12:36:29
回答 2查看 2.5K关注 0票数 1

我正在一个IIS 6应用程序上运行一个Rest应用程序,调用ASP.NET脚本来执行特定的任务。它在我的笔记本电脑(Windows10)上工作得很好,但当我在WindowsServer2019版本1809版本Build 17763.1935上运行它时,它就不能工作了。错误告诉我,它找不到程序集"Microsoft.Management.Infrastructure“。

未处理的异常。System.IO.FileNotFoundException:无法加载文件或程序集'Microsoft.Management.Infrastructure,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35‘。Das系统,kann,die,angegebene,Datei,nicht .文件名:'Microsoft.Management.Infrastructure,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35‘

"Das System kann die angegebene Datei nicht finden.“=”文件未找到“

有人也遇到过这个问题吗?服务器安装了以下功能:

  • MicrosoftWindows6.0.3-WindowsServer托管.NET .NET运行时
  • 6.0.3 (x64)微软.NET运行时- 6.0.3 (x86)
  • 微软.NET SDK 6.0.201 (x64)
  • ASP.NET核心6.0.3 -共享框架(x64)
  • 微软ASP.NET核心6.0.3 -共享框架(x86)
  • 微软Visual C++ 2015-2019可再发行版(x64) - 14.28.29913
  • 微软Visual C++ 2015-2019可再发行版(x86) - 14.28.29913
  • IIS 10.0
  • Windows PowerShell 5.1
  • PowerShell 7.2.1

现在,为了测试是否是服务器设置丢失了什么,我用下面的代码编写了一个小的.net控制台应用程序

代码语言:javascript
复制
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\文件夹中的每个DLL (用于在我的笔记本上进行测试并运行良好)也包含在bin\文件夹中(发布到该文件夹中的DLL)。发布文件夹中有DLL,但调试文件夹中没有:

  • 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

文件"Microsoft.Management.Infrastructure.dll“既不在发行版中,也不在调试文件夹中。

项目csproj文件如下所示:

代码语言:javascript
复制
<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文件

代码语言:javascript
复制
<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版本。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-21 10:05:31

我通过在目标服务器上构建/发布应用程序来解决这个问题。项目或代码中没有任何更改。IIS也保持不变。在服务器上本地构建它之后,它现在才能工作。

票数 0
EN

Stack Overflow用户

发布于 2022-10-04 15:15:17

我发现目标运行时很重要。对于一般的win-x64运行时,这是行不通的,但win10-x64是这样做的。

csproj中的这两个标志:

代码语言:javascript
复制
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>

而发布命令中的这些开关:

代码语言:javascript
复制
dotnet publish --sc --runtime win10-x64

我在Windows作为PowerShell服务 on GitHub中使用上面的内容做了一个小项目

下面是我用来让powershell工作的最新参考资料:

代码语言:javascript
复制
<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" />
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71527116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档