Install or not install a feature based on OS version

I want to have a configuration that installs WMF 5.1 if the server requires it (i.e. Windows Server 2012 R2 but not 2016). But I’m having trouble writing a configuration that makes that decision at the time the MOF runs rather than at compile time. For example if you tried this:

configuration Windows {
    param (
        $ComputerName
    )

    node $ComputerName {
        [version] $version = Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty Version
        if ($version -ge [version] "6.3" -and $version -lt [version] "10.0") {
            xHotfixInstall HotfixWMF51 {
                Ensure = "Present"
                Path   = "Win8.1AndW2K12R2-KB3191564-x64.msu"
                Id     = "KB3191564"
            }
        } else {
            Write-Host "Not including WMF 5.1 support"
        }
    }
}

It gets evaluated at compile time instead of later. Which isn’t great when you’re going to transfer that MOF to a remote server to run.

Is there a different way this should be handled in DSC?

I’ve found the Script / GetScript / TestScript / SetScript sections.

But it looks like SetScript doesn’t allow you to use a resource like xHotFixInstall… right?

The only way to currently handle conditions is to create a custom resource. The Get/Test would probably be pretty much the same but the Set would have an If statement to evaluate the OS version and install as needed. That said, I have never tried updating WMF using Windows DSC. It could be problematic if services are trying to restart in to a new version in the middle of a configuration.

By the way, this UserVoice item is what we are currently tracking as votes for “conditional logic”:
https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11088639-enable-service-restart-and-similar-scenarios-in-dsc

I’ve tried using DSC with xHotfix to upgrade WMF 4.0 to WMF 5.0. It was not successful. Too many changes in DSC between the two versions. You may have better luck with 5.1 though.

One way to work around the problem is to use Chocolatey to abstract the problem/logic away (i.e. Using the cChoco DSC Resource).

I haven’t checked the code in details, but maybe the powershell package already handles that for you: https://chocolatey.org/packages/PowerShell

Basically your DSC Config will declare the powershell package as required, and when installing, it will check if it’s already running v5, or if it should install the hotfixes/msu for that OS version.

That said, I recommend treating wmf5.1 as a pre-req for DSC when possible (apply retrospectively ad-hoc before managing with DSC and update OS deployment task sequences), to avoid problems with the different versions between v4, v4 ~mark2~ (KB3000850) and v5(.1)…