运行 Start-DSCConfiguration 时出现错误:无法加载文件或程序集“System.Management.Automation”,版本为7.1.0.0。

4

运行期望状态配置命令:

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

我们收到以下错误:

InvalidOperation: 导入模块cSharpDSCResource时失败,错误为-无法加载文件或程序集“System.Management.Automation,Version=7.1.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其某个依赖项。系统找不到指定的文件。

dll的目录结构:

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

[appdomain] :: CurrentDomain.GetAssemblies() | Sort-Object -Property FullName | Select-Object -Property FullName; 从$PSVersionTable.PSVersion 5提示返回:

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

[appdomain] :: CurrentDomain.GetAssemblies() | Sort-Object -Property FullName | Select-Object -Property FullName; 从$PSVersionTable.PSVersion 7提示返回:

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

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

Get-DSCResource返回:

Binary cSharpDSCResource cSharpDSCResource 0.0.1 {Path, Content, DependsOn, Ensure...}

我们正在使用的是Windows Management Framework:

Microsoft Net SDK 5.0.101

Visual Studio Code中项目的代码:

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 - Panagiotis Kanavos
是的,我已经安装了PowerShell 7.1。在查找后发现,PowerShell Core 7.1 是 Powershell 6.0 的升级版(两者都使用 .Net Core)。我使用 Powershell 7.1 提示符运行 Start-DSCConfiguration 命令,但返回了相同的错误。 - scranley
你使用什么操作系统? - T-Me
Windows Server 2019标准版,版本1809 - scranley
尝试使用netcoreapp3.1作为TargetFramework,但结果相同。 - scranley
2个回答

1

有一种软件可以帮助你分析为什么无法加载某些依赖项。

例如,Dependency Walker 可以提供一些关于为什么无法加载 dll 的见解。这是我用来查找加载 dll 问题的首选工具。往往它会失败地加载一些依赖项的依赖项。


我尝试使用依赖项查看器。一旦加载了dll,鼠标指针就会一直旋转(窗口无响应)。 - scranley
是的,它相当缓慢,可能需要几分钟才能完成。你也可以在互联网上找到替代工具。本质上,有软件可以帮助你找出为什么无法加载依赖项。我会在我的答案中提到这一点。 - Tyron

0
在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。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接