Search code examples
c#.netpowershellresourcesdsc

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


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>


Solution

  • 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