Solving DSC Problems on Windows 10 & Writing PowerShell Code that writes PowerShell Code for you

I recently ran into a problem with DSC on Windows 10 when trying to create MOF files with DSC configurations that work on other operating systems. An error is generated when the friendly name for a DSC resource contains a dash and that friendly name is specified as a dependency for another resource. I know that only certain characters are allowed in the name that's specified for DependsOn and I've run into similar problems with things such as IP addresses due to the dot or period, but the dash works in other operating systems at least with the production preview of PowerShell version 5, but not with the version of PowerShell version 5 that ships with Windows 10:

 1configuration ConfigureSQLServer {
 2
 3    Import-DscResource -ModuleName PSDesiredStateConfiguration, xSqlPs
 4
 5    node Server01 {
 6        WindowsFeature Net-Framework-Core {
 7            Name = 'Net-Framework-Core'
 8            Ensure = 'Present'
 9        }
10
11        xSqlServerInstall InstallSQLEngine {
12            InstanceName = 'MSSQLSERVER'
13            SourcePath = 'D:'
14            Features= 'SQLEngine'
15            DependsOn ='[WindowsFeature]Net-Framework-Core'
16        }
17    }
18}

win10-dsc1a.jpg

 1Test-DependsOn : The format of the resource reference ‘[WindowsFeature]Net-Framework-Core’ in the Requires list for
 2resource ‘[xSqlServerInstall]InstallSQLEngine’ is not valid. A required resource name should be in the format
 3‘[<typename>]<name>’, with no spaces.
 4At line:117 char:13
 5+ Test-DependsOn
 6+ ~~~~~~~~~~~~~~
 7+ CategoryInfo : InvalidOperation: (:) [Write-Error], ParentContainsErrorRecordException
 8+ FullyQualifiedErrorId : GetBadlyFormedRequiredResourceId,Test-DependsOn
 9
10Errors occurred while processing configuration ‘ConfigureSQLServer’.
11At
12C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:3588
13char:5
14+ throw $ErrorRecord
15+ ~~~~~~~~~~~~~~~~~~
16+ CategoryInfo : InvalidOperation: (ConfigureSQLServer:String) [], InvalidOperationException
17+ FullyQualifiedErrorId : FailToProcessConfiguration

Modifying the configuration to fix the problem for this example isn't difficult since only one feature is specified:

 1configuration ConfigureSQLServer {
 2
 3    Import-DscResource -ModuleName PSDesiredStateConfiguration, xSqlPs
 4
 5    node Server01 {
 6        WindowsFeature NetFrameworkCore {
 7            Name = 'Net-Framework-Core'
 8            Ensure = 'Present'
 9        }
10
11        xSqlServerInstall InstallSQLEngine {
12            InstanceName = 'MSSQLSERVER'
13            SourcePath = 'D:'
14            Features= 'SQLEngine'
15            DependsOn ='[WindowsFeature]NetFrameworkCore'
16        }
17    }
18}

win10-dsc2a.jpg

This becomes a bigger problem when lots of Windows Features are added to the configuration. Maybe I'm building an additional SQL Server and I want to install all of the features that are currently installed on an existing SQL Server.

Note: This would only be a problem if they had dependencies set in the configuration, but maybe I want to be proactive and eliminate the dashes to prevent problems moving forward and not each time that I add a dependency.

1Invoke-Command -ComputerName SQL04 {
2    Get-WindowsFeature | Where-Object Installed -eq $True
3} | Select-Object -ExpandProperty Name

win10-dsc3a.jpg

That existing SQL Server currently has 14 roles that are enabled. That would require a lot of manual tweaking to remove all of the dashes as demonstrated in the previous example. What I can do though is to use PowerShell to generate the code for that portion of the configuration and paste it in instead of having to write it all manually:

1Invoke-Command -ComputerName SQL04 {
2    Get-WindowsFeature | Where-Object Installed -eq $True
3} | Select-Object -ExpandProperty Name |
4ForEach-Object {
5"        WindowsFeature $($_ -replace '-','') {"
6"            Name = '$_'"
7"            Ensure = 'Present'"
8"        }"
9} | clip.exe

Although this configuration is valid, it contains a lot of somewhat static code to maintain. There has to be a better way, but if you're going to do it this way you might as well at least have PowerShell write some of the code for you.

 1configuration ConfigureSQLServer {
 2
 3    Import-DscResource -ModuleName PSDesiredStateConfiguration, xSqlPs
 4
 5    node Server01 {
 6        WindowsFeature FileAndStorageServices {
 7            Name = 'FileAndStorage-Services'
 8            Ensure = 'Present'
 9        }
10        WindowsFeature StorageServices {
11            Name = 'Storage-Services'
12            Ensure = 'Present'
13        }
14        WindowsFeature NETFrameworkFeatures {
15            Name = 'NET-Framework-Features'
16            Ensure = 'Present'
17        }
18        WindowsFeature NETFrameworkCore {
19            Name = 'NET-Framework-Core'
20            Ensure = 'Present'
21        }
22        WindowsFeature NETFramework45Features {
23            Name = 'NET-Framework-45-Features'
24            Ensure = 'Present'
25        }
26        WindowsFeature NETFramework45Core {
27            Name = 'NET-Framework-45-Core'
28            Ensure = 'Present'
29        }
30        WindowsFeature NETWCFServices45 {
31            Name = 'NET-WCF-Services45'
32            Ensure = 'Present'
33        }
34        WindowsFeature NETWCFTCPPortSharing45 {
35            Name = 'NET-WCF-TCP-PortSharing45'
36            Ensure = 'Present'
37        }
38        WindowsFeature MultipathIO {
39            Name = 'Multipath-IO'
40            Ensure = 'Present'
41        }
42        WindowsFeature FSSMB1 {
43            Name = 'FS-SMB1'
44            Ensure = 'Present'
45        }
46        WindowsFeature UserInterfacesInfra {
47            Name = 'User-Interfaces-Infra'
48            Ensure = 'Present'
49        }
50        WindowsFeature PowerShellRoot {
51            Name = 'PowerShellRoot'
52            Ensure = 'Present'
53        }
54        WindowsFeature PowerShell {
55            Name = 'PowerShell'
56            Ensure = 'Present'
57        }
58        WindowsFeature WoW64Support {
59            Name = 'WoW64-Support'
60            Ensure = 'Present'
61        }
62        xSqlServerInstall InstallSQLEngine {
63            InstanceName = 'MSSQLSERVER'
64            SourcePath = 'D:'
65            Features= 'SQLEngine'
66            DependsOn ='[WindowsFeature]NetFrameworkCore'
67        }
68    }
69}

win10-dsc4a.jpg

Let's try separating the environmental configuration from the structural configuration to see if that reduces the amount of what seems to be redundant code. The structural configuration is very concise:

 1configuration ConfigureSQLServer {
 2
 3    Import-DscResource -ModuleName PSDesiredStateConfiguration, xSqlPs
 4
 5    node $AllNodes.NodeName {
 6
 7        $Node.WindowsFeature.ForEach({
 8            WindowsFeature $_.ID  {
 9                Name = $_.Name
10                Ensure = 'Present'
11            }
12        })
13
14        xSqlServerInstall InstallSQLEngine {
15            InstanceName = $Node.InstanceName
16            SourcePath = $Node.SourcePath
17            Features= $Node.Features
18            DependsOn = '[WindowsFeature]NetFrameworkCore'
19        }
20    }
21}

For the environmental configuration, we'll need to create a hash table for each of the Windows Features that we want to install. We'll use PowerShell again to write all of those hash tables for us:

1Invoke-Command -ComputerName SQL04 {
2    Get-WindowsFeature | Where-Object Installed -eq $True
3} | Select-Object -ExpandProperty Name |
4ForEach-Object {
5"                @{"
6"                    ID = '$($_ -replace '-','')'"
7"                    Name = '$_'"
8"                }"
9} | clip.exe

Then simply paste that portion of the code instead of having to manually type it all in manually:

 1$ConfigData = @{
 2    AllNodes = @(
 3        @{
 4            NodeName = 'SQL04'
 5            InstanceName = 'MSSQLSERVER'
 6            SourcePath =  'D:'
 7            Features = 'SQLEngine'
 8            WindowsFeature = @(
 9                @{
10                    ID = 'FileAndStorageServices'
11                    Name = 'FileAndStorage-Services'
12                }
13                @{
14                    ID = 'StorageServices'
15                    Name = 'Storage-Services'
16                }
17                @{
18                    ID = 'NETFrameworkFeatures'
19                    Name = 'NET-Framework-Features'
20                }
21                @{
22                    ID = 'NETFrameworkCore'
23                    Name = 'NET-Framework-Core'
24                }
25                @{
26                    ID = 'NETFramework45Features'
27                    Name = 'NET-Framework-45-Features'
28                }
29                @{
30                    ID = 'NETFramework45Core'
31                    Name = 'NET-Framework-45-Core'
32                }
33                @{
34                    ID = 'NETWCFServices45'
35                    Name = 'NET-WCF-Services45'
36                }
37                @{
38                    ID = 'NETWCFTCPPortSharing45'
39                    Name = 'NET-WCF-TCP-PortSharing45'
40                }
41                @{
42                    ID = 'MultipathIO'
43                    Name = 'Multipath-IO'
44                }
45                @{
46                    ID = 'FSSMB1'
47                    Name = 'FS-SMB1'
48                }
49                @{
50                    ID = 'UserInterfacesInfra'
51                    Name = 'User-Interfaces-Infra'
52                }
53                @{
54                    ID = 'PowerShellRoot'
55                    Name = 'PowerShellRoot'
56                }
57                @{
58                    ID = 'PowerShell'
59                    Name = 'PowerShell'
60                }
61                @{
62                    ID = 'WoW64Support'
63                    Name = 'WoW64-Support'
64                }
65            )
66        }
67    )
68}

win10-dsc5a.jpg

That's still an enormous amount of somewhat redundant code to have to maintain. There must be a better way than having so many lines of code for each feature so let's try going another route. We'll add a small amount of complexity to our configuration so the dash (-) can be stripped out on the fly using the replace operator. The replace method could also be used. I generally prefer to use the replace operator instead of the replace method since the replace method is case sensitive and the replace operator is not but it doesn't matter in this scenario.

 1configuration ConfigureSQLServer {
 2
 3    Import-DscResource -ModuleName PSDesiredStateConfiguration, xSqlPs
 4
 5    node $AllNodes.NodeName {
 6
 7        $Node.WindowsFeature.ForEach({
 8            WindowsFeature ($_ -replace '-','') {
 9                Name = $_
10                Ensure = 'Present'
11            }
12        })
13        xSqlServerInstall InstallSQLEngine {
14            InstanceName = $Node.InstanceName
15            SourcePath = $Node.SourcePath
16            Features= $Node.Features
17            DependsOn = '[WindowsFeature]NetFrameworkCore'
18        }
19    }
20}

Let's use PowerShell to generate a comma separated list of Windows Features based on the ones that are enabled on SQL04. I've seen people write an enormous amount of code to accomplish something like this which is simple to do:

1(Invoke-Command -ComputerName SQL04 {
2    Get-WindowsFeature | Where-Object Installed -eq $True
3}).Name -join "','" |
4clip.exe

I'll use that comma separated list to create a new more simplistic environmental configuration:

 1$ConfigData = @{
 2    AllNodes = @(
 3        @{
 4            NodeName = 'SQL04'
 5            InstanceName = 'MSSQLSERVER'
 6            SourcePath =  'D:'
 7            Features = 'SQLEngine'
 8            WindowsFeature = 'FileAndStorage-Services','Storage-Services','NET-Framework-Features','NET-Framework-Core',
 9                             'NET-Framework-45-Features','NET-Framework-45-Core','NET-WCF-Services45','NET-WCF-TCP-PortSharing45',
10                             'Multipath-IO','FS-SMB1','User-Interfaces-Infra','PowerShellRoot','PowerShell','WoW64-Support'
11        }
12    )
13}

win10-dsc6a.jpg

You could also place code to generate the list of features directly inside the environmental configuration as long as you're storing the hash table in a variable:

 1$ConfigData = @{
 2    AllNodes = @(
 3        @{
 4            NodeName = 'SQL04'
 5            InstanceName = 'MSSQLSERVER'
 6            SourcePath =  'D:'
 7            Features = 'SQLEngine'
 8            WindowsFeature = (Invoke-Command -ComputerName SQL04 {
 9                                Get-WindowsFeature | Where-Object Installed -eq $True
10                             } | Select-Object -ExpandProperty Name)
11        }
12    )
13}

win10-dsc7a.jpg

If you're going to store the hash table for the environmental configuration in a PSD1 file, you can't use PowerShell code such as what's shown in the previous example. The hash table has to be static when it's saved in a PSD1 file.

 1New-Item -Path .\configdata.psd1 -ItemType File -Force -Value "
 2@{
 3    AllNodes = @(
 4        @{
 5            NodeName = 'SQL04'
 6            InstanceName = 'MSSQLSERVER'
 7            SourcePath =  'D:'
 8            Features = 'SQLEngine'
 9            WindowsFeature = 'FileAndStorage-Services','Storage-Services','NET-Framework-Features','NET-Framework-Core',
10                             'NET-Framework-45-Features','NET-Framework-45-Core','NET-WCF-Services45','NET-WCF-TCP-PortSharing45',
11                             'Multipath-IO','FS-SMB1','User-Interfaces-Infra','PowerShellRoot','PowerShell','WoW64-Support'
12        }
13    )
14}"

win10-dsc8a.jpg

Create the MOF file using the configuration data stored in a PSD1 file:

win10-dsc9a.jpg

Mark Twain once said “I didn't have time to write a short letter, so I wrote a long one instead.” That's similar to how writing DSC configurations work.

A quick and dirty DSC configuration is often long and complicated because making them short and simple requires a little more time and effort. That additional time and effort will pay off big time in the long run though by saving your sanity when you have to revisit or troubleshoot it in the future.

µ