4

When running Desired State Configuration command:

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

We get the following error:

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.

The directory structure to the 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

We can't figure out why it cannot find or load System.Management.Automation 7.1.0.0

Get-DSCResource Returns:

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

We are using windows management framework:

Microsoft Net SDK 5.0.101

Code for project in 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>

scranley
  • 188
  • 1
  • 4
  • 20
  • Does the target machine have PowerShell Core 7.1 installed? If not, you can't use any PowerShell 7.1 DSCs on it. If you have PS 7.1, did you use a PowerShell Core prompt to run `Start-DSCConfiguration` ? – Panagiotis Kanavos Jan 05 '21 at 16:26
  • Yes I have Powershell 7.1 installed which after looking it up, PowerShell Core 7.1 is an upgrade to Powershell 6.0 (both use .Net Core). I used the Powershell 7.1 prompt to run Start-DSCConfiguration and returned the same error. – scranley Jan 05 '21 at 17:13
  • What OS do you use? – T-Me Jan 11 '21 at 08:12
  • Windows Server 2019 Standard, version 1809 – scranley Jan 11 '21 at 16:26
  • Tried netcoreapp3.1 as TargetFramework with same result. – scranley Jan 14 '21 at 23:58

2 Answers2

1

There is software that lets you analyze why some dependencies cannot be loaded.

For example Dependency Walker could provide some insight on why it cannot load the dll. It my go-to tool for finding problems loading dlls. More often than not it fails to load some dependencies of dependencies.

Tyron
  • 1,938
  • 11
  • 30
  • I tried using dependency walker. Once the dll is loaded, the mouse pointer just spins (window not responding). – scranley Jan 12 '21 at 00:37
  • Yea its rather slow, might take several minutes to complete. You might also find alternative tools in the internet. In essence, there is software that can help you figure out why a dependency cannot be loaded. I'll mentioned that in my answer. – Tyron Jan 12 '21 at 11:12
0

In visual studio, create a new Net Framework project called cSharpDSCResourceExample.

Use Tools->NuGet Package manager and install Microsoft.PowerShell.5.1.ReferenceAssemblies.

Install the package Microsoft.PowerShell.5.1.ReferenceAssemblies (NuGet) This package name appears in the documentation of classes in the namespace System.Management.Automation.

Once the dll is compiled copy the files in the debug directory to:

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

This will include System.Management.Automation.dll

scranley
  • 188
  • 1
  • 4
  • 20