运行 Start-DSCConfiguration 时出错:无法加载文件或程序集 'System.Management.Automation',版本=7.1.0.0

Error when running Start-DSCConfiguration: Could not load file or assembly 'System.Management.Automation', Version=7.1.0.0

当运行 Desired State Configuration 命令时:

Start-DSCConfiguration -ComputerName localhost -Path ./CsharpExample -Verbose -Wait -Force

我们收到以下错误:

InvalidOperation: Importing module cSharpDSCResource failed with error - Could not load file or assembly 'System.Management.Automation, Version=7.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

dll 的目录结构:

"C:\Program Files\WindowsPowerShell\Modules\cSharpDSCResource\DSCResources\cSharpDSCResource\cSharpDSCResource.dll"

[appdomain]::CurrentDomain.GetAssemblies() | Sort-Object -Property FullName | Select-Object -Property FullName; returns from $PSVersionTable.PSVersion 5 prompt:

System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

[appdomain]::CurrentDomain.GetAssemblies() | Sort-Object -Property FullName | Select-Object -Property FullName; returns from $PSVersionTable.PSVersion 7 prompt:

System.Management.Automation, Version=7.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

我们无法弄清楚为什么它无法找到或加载 System.Management.Automation 7.1.0.0

获取 DSCResource Returns:

二进制 cSharpDSCResource cSharpDSCResource 0.0.1 {路径、内容、DependsOn、确保...}

我们正在使用windows管理框架:

微软网络 SDK 5.0.101

visual studio 代码中的项目代码:

cSharpDSCresource.csproj

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Management.Automation" Version="7.1.0" />
  </ItemGroup>

有软件可以让你分析为什么有些依赖无法加载。

例如 Dependency Walker 可以提供一些关于为什么它无法加载 dll 的见解。它是我查找加载 dll 问题的首选工具。很多时候它加载依赖的一些依赖失败。

在 visual studio 中,创建一个名为 cSharpDSCResourceExample 的新 Net Framework 项目。

使用工具->NuGet 包管理器并安装 Microsoft.PowerShell.5.1.ReferenceAssemblies.

安装包 Microsoft.PowerShell.5.1.ReferenceAssemblies (NuGet) 此包名称出现在命名空间 System.Management.Automation.

中 类 的文档中

编译dll后,将调试目录中的文件复制到:

C:\程序Files\WindowsPowerShell\Modules\cSharpDSCResourceExample\DSCResources\cSharpDSCResourceExample

这将包括 System.Management.Automation.dll