winget-cli icon indicating copy to clipboard operation
winget-cli copied to clipboard

winget configure fails when calling WindowsOptionalFeature DSC Resource

Open jcwalker opened this issue 2 years ago • 11 comments

Brief description of your issue

When a winget configuration calls the DSC resource PSDscResources/WindowsOptionalFeature it will error with with message Class not registered

Steps to reproduce

Author a configuration with the following resource:

  - resource: PSDscResources/WindowsOptionalFeature
    id: removeWindowsMediaPlayer
    directives:
      description: Uninstall Windows Media removeWindowsMediaPlayer
    settings:
      name: WindowsMediaPlayer
      ensure: Absent 

Expected behavior

Configuration succeeds and the optional feature is removed.

Actual behavior

Errors with Class not registered

image

From the winget logs this is happening on line 286 of the DSC resource image

image

When running the same command in PowerShell the command succeeds without error: image

Is winget using a different version of Dism than the PowerShell module is?

This happens on Windows 10 and Windows 11, PowerShell version 5.1 or 7.4

Environment

Windows Package Manager v1.7.10582
Windows: Windows.Desktop v10.0.22631.3296
System Architecture: X64
Package: Microsoft.DesktopAppInstaller v1.22.10582.0

jcwalker avatar Mar 12 '24 19:03 jcwalker

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar Mar 12 '24 19:03 github-actions[bot]

I ran into this same problem with the Windows Sandbox. It looks like it's happening to the way the DISM APIs are being called. We recently refactored our WinGet dependency feature to call DISM directly.

I did find a bit of a workaround by using the Microsoft.WinGet.Configuration module (requires PowerShell 7).

https://github.com/microsoft/devhome/tree/main/docs/sampleConfigurations/DscResources/Microsoft.WindowsSandbox.DSC#microsoftwindowssandboxdsc

denelon avatar Mar 12 '24 19:03 denelon

@denelon thanks for the quick reply! I can confirm that workaround work.

jcwalker avatar Mar 12 '24 19:03 jcwalker

  • Related to #3348

Trenly avatar Mar 13 '24 02:03 Trenly

I am getting the same error when using this YAML:

properties:
  resources:
    - resource: PSDscResources/WindowsOptionalFeature
      settings:
        name: VirtualMachinePlatform
        ensure: Present

Error in the log file: 2024-04-02 15:56:41.341 [CONF] Microsoft.Management.Configuration.Processor.Exceptions.InvokeDscResourceException: Failed when calling `Test` for resource: WindowsOptionalFeature [PSDscResources] Message: 'Class not registered ' It appears @denelon's workaround does work, but using winget configure to install PowerShell 7 on a fresh machine beforehand is a bit of a pain.

As a side note, it seems that installing and setting up WSL is a very common use case for winget configure, which currently requires multiple steps in the YAML file to get working (enable Windows Virtual Machine Platform, install WSL, install desired Linux distro). It would be great if there was a DSC module which just does all this for you.

phueac avatar Apr 02 '24 15:04 phueac

Could someone maybe explain how to apply a WindowsOptionalFeature through winget configuration with @denelon's workaround?

eluinstra avatar Feb 09 '25 20:02 eluinstra

Could someone maybe explain how to apply a WindowsOptionalFeature through winget configuration with @denelon's workaround?

I did not manage to figure out what that specific workaround was supposed to be, but this snippet below leverages the Script resource to ensure VirtualMachinePlatform is enabled. It's admittedly less elegant, but it produces the desired outcome and you should be able to adapt it for other interactions with optional features.

Hope this will still be useful to you (or someone else)!

# yaml-language-server: $schema=https://aka.ms/configuration-dsc-schema/0.2

properties:
  configurationVersion: "0.2.0"
  resources:
    - resource: PSDscResources/Script
      id: EnableVirtualMachinePlatform
      directives:
        description: Enabling VirtualMachinePlatform using DISM
      settings:
        GetScript: |
          return $true
        TestScript: |
          $output = dism.exe /online /get-featureinfo /featurename:VirtualMachinePlatform
          if ($output -match 'State : Enabled') {
              return $true
          } else {
              return $false
          }
        SetScript: |
          dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

swiftsvre avatar Feb 27 '25 09:02 swiftsvre

Something else that might help here is the "securityContext: elevated" directive. WinGet 1.10 can allow a configuration started as "user" provide a single UAC upon the agreement, and given permissions it will run the resources decorated this way run elevated while the others still run in user context.

denelon avatar Feb 27 '25 19:02 denelon

Could someone maybe explain how to apply a WindowsOptionalFeature through winget configuration with @denelon's workaround?

@eluinstra @swiftsvre Here's what i did:

  • Open PowerShell 7 as admin
  • Install Microsoft.WinGet.Configuration module Install-PSResource -Name Microsoft.WinGet.Configuration
  • Apply the configuration:
$result = Get-WinGetConfiguration -File YourFile.dsc.yaml | Invoke-WinGetConfiguration
$result.UnitResults | Where-Object {$_.Type -eq 'WindowsOptionalFeature'}
>>$result.UnitResults | Where-Object {$_.Type -eq 'WindowsOptionalFeature'}

State                    : Completed
PreviouslyInDesiredState : True
RebootRequired           : False
Type                     : WindowsOptionalFeature
ResultCode               : 0
Message                  :
Description              :
Details                  :

miscalibrated avatar Feb 28 '25 01:02 miscalibrated

@swiftsvre : Thanks, that works.

@denelon : That helped to run the script in a normal shell and only elevate the command

@connorcarnes : I tried it with the following script:

# yaml-language-server: $schema=https://aka.ms/configuration-dsc-schema/0.2
properties:
  configurationVersion: "0.2.0"
  resources:
  - resource: PSDscResources/WindowsOptionalFeature
      id: TFTP
      # directives:
      #   securityContext: elevated
      settings:
        name: TFTP
        ensure: Present

using the following command:

$result = Get-WinGetConfiguration -File YourFile.dsc.yaml | Invoke-WinGetConfiguration
$result.UnitResults | Where-Object {$_.Type -eq 'WindowsOptionalFeature'}

but unfortunately it still results in a Class not registered error:

State                    : Completed
PreviouslyInDesiredState : False
RebootRequired           : False
Type                     : WindowsOptionalFeature
ResultCode               : -1978285819
Message                  : The configuration unit failed while attempting to
                           test the current system state.
Description              : Class not registered
Details                  : at Test-TargetResource, C:\Users\eluin\AppData\Local
                           \Microsoft\WinGet\Configuration\Modules\PSDscResourc
                           es\2.12.0.0\DscResources\MSFT_WindowsOptionalFeature
                           \MSFT_WindowsOptionalFeature.psm1: line 286
                           at Invoke-DscScriptBasedResource, C:\Users\eluin\App
                           Data\Local\Microsoft\WinGet\Configuration\Modules\PS
                           DesiredStateConfiguration\2.0.7\PSDesiredStateConfig
                           uration.psm1: line 4801
                           at Invoke-DscResource, C:\Users\eluin\AppData\Local\
                           Microsoft\WinGet\Configuration\Modules\PSDesiredStat
                           eConfiguration\2.0.7\PSDesiredStateConfiguration.psm
                           1: line 4716

eluinstra avatar Feb 28 '25 23:02 eluinstra

Just run into this, the securityContext: elevated have not helped (UAC shown as expected), so I rewrote it to Script resource by calling PowerShell Enable-WindowsOptionalFeature, but got the same Class not registered error message. When I call the same command from the elevated PowerShell manually, it works. So this has to have something related to how the elevated environment is entered by the WinGet.

Thanks for dism version @swiftsvre, really nice, although I think the GetScript returns wrong format. This is the version I am using now:

    - resource: PSDscResources/Script
      id: EnableVirtualMachinePlatform
      directives:
        description: Enabling VirtualMachinePlatform using DISM
      settings:
        GetScript: |
          @{ Result = (& {
            $out = & dism.exe /online /get-featureinfo /featurename:VirtualMachinePlatform 2>&1
            [bool]($out -match 'State\s*:\s*Enabled')
          }) }
        TestScript: |
          return (& {
            $out = & dism.exe /online /get-featureinfo /featurename:VirtualMachinePlatform 2>&1
            [bool]($out -match 'State\s*:\s*Enabled')
          })
        SetScript: |
          & dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart | Out-Null

oldium avatar Jan 02 '26 09:01 oldium