【问题标题】:Error when running Start-DSCConfiguration: Could not load file or assembly 'System.Management.Automation', Version=7.1.0.0运行 Start-DSCConfiguration 时出错:无法加载文件或程序集“System.Management.Automation”,版本 = 7.1.0.0
【发布时间】:2021-04-10 18:53:22
【问题描述】:

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

Get-DSCResource 返回:

二进制 cSharpDSCResource cSharpDSCResource 0.0.1 {Path, Content, DependsOn, Ensure...}

我们正在使用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>

【问题讨论】:

  • 目标机器是否安装了 PowerShell Core 7.1?如果不是,则不能在其上使用任何 PowerShell 7.1 DSC。如果您有 PS 7.1,您是否使用 PowerShell Core 提示符运行 Start-DSCConfiguration
  • 是的,我安装了 Powershell 7.1,在查找之后,PowerShell Core 7.1 是对 Powershell 6.0 的升级(均使用 .Net Core)。我使用 Powershell 7.1 提示符运行 Start-DSCConfiguration 并返回相同的错误。
  • 你使用什么操作系统?
  • Windows Server 2019 标准版,版本 1809
  • 尝试 netcoreapp3.1 作为 TargetFramework,结果相同。

标签: c# .net powershell resources dsc


【解决方案1】:

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

例如Dependency Walker 可以提供一些关于为什么它无法加载 dll 的见解。它是我查找加载 dll 问题的首选工具。通常它无法加载依赖项的某些依赖项。

【讨论】:

  • 我尝试使用依赖walker。一旦加载了 dll,鼠标指针就会旋转(窗口没有响应)。
  • 是的,它相当慢,可能需要几分钟才能完成。您还可以在 Internet 上找到替代工具。本质上,有一些软件可以帮助您找出无法加载依赖项的原因。我会在回答中提到这一点。
【解决方案2】:

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

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

安装包 Microsoft.PowerShell.5.1.ReferenceAssemblies (NuGet) 此包名称出现在命名空间 System.Management.Automation 中的类文档中。

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

C:\Program Files\WindowsPowerShell\Modules\cSharpDSCResourceExample\DSCResources\cSharpDSCResourceExample

这将包括 System.Management.Automation.dll

【讨论】:

    猜你喜欢
    • 2015-02-15
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多