PowerShell Desired State Configuration Error: Undefined Property ConfigurationName

The scenario in this blog article is that you've created a DSC configuration on a computer that's running a preview of PowerShell version 5. The machine itself could be running Windows 8.1, Server 2012 R2, or the Windows Technical Preview version of Windows 10.

Here's a simple configuration that I'll use to demonstrate the problem. This DSC configuration uses a custom DSC resource to rename a computer. This configuration is being created on a machine running the Windows Technical Preview version of Windows 10 with PowerShell version 5 build 9860 (the most recent build as of this writing).

 1configuration ConfigureNewServer {
 2
 3    Import-DscResource -ModuleName cMrComputerName
 4
 5    node 192.168.29.107 {
 6
 7        cMrComputerName Name {
 8            ComputerName = 'Server01'
 9        }
10    }
11}

A new Windows 2012 R2 Server (running PowerShell v4) has been brought online and no configuration has been done to it so far. The IP address of that server is 192.168.29.107. That IP address has been added to the TrustedHosts on the Windows 10 workstation that is being used to create the DSC configuration.

The configuration is run and the MOF file is generated for the server as shown in the following example:

1ConfigureNewServer

dsc-error1.jpg

An attempt to apply the configuration to the server is made, but an error is generated:

1Start-DscConfiguration -Path .\ConfigureNewServer -Wait -Verbose -Credential (Get-Credential)

dsc-error2.jpg

1Undefined property ConfigurationName
2At line:16, char:2
3Buffer:
4onfigureNewServer”;
5};^
6insta
7+ CategoryInfo : SyntaxError: (root/Microsoft/…gurationManager:String) [], CimException
8+ FullyQualifiedErrorId : MiClientApiError_Failed
9+ PSComputerName : 192.168.29.107

Since I'm using a custom DSC resource that I wrote, you might think that the error has to do with it, but this error would occur even if one of the Microsoft supplied DSC resources was being used.

The issue is that there's a couple of extra lines in the MOF files generated by a machine running PowerShell version 5 which seems to make the MOF files incompatible with a machine running PowerShell version 4.

The following MOF configuration file was created on a machine running PowerShell v5:

dsc-error3.jpg

Here's the same MOF file created on a machine running PowerShell version 4:

dsc-error4.jpg

Removing those two lines allows the configuration to complete successfully without issue. For a single MOF file manually modifying it probably wouldn't be an issue, but what if you had dozens or more? Then there's the question as to why you would want to do anything manually that you could use PowerShell to automate? After all, that is the purpose isn't it?

Here's a function to remove those two lines from one or more MOF files:

 1#Requires -Version 4.0
 2function ConvertTo-MrMOFv4 {
 3
 4    [CmdletBinding()]
 5    param (
 6        [Parameter(Mandatory,
 7                   ValueFromPipelineByPropertyName)]
 8        [ValidateScript({Test-Path $_ -PathType Leaf -Include *.mof})]
 9        [Alias('FullName')]
10        [string[]]$Path,
11
12        [Parameter(DontShow)]
13        [ValidateNotNullorEmpty()]
14        [string]$Pattern = '^\sName=.*;$|^\sConfigurationName\s=.*;$'
15    )
16
17    PROCESS {
18        foreach ($file in $Path) {
19
20            $mof = Get-Content -Path $file
21
22            if ($mof -match $Pattern) {
23                Write-Verbose -Message "PowerShell v4 compatibility problems were found in file: $file"
24
25                try {
26                    $mof -replace $Pattern |
27                    Set-Content -Path $file -Force -ErrorAction Stop
28                }
29                catch {
30                    Write-Warning -Message "An error has occurred. Error details: $_.Exception.Message"
31                }
32                finally {
33                    if ((Get-Content -Path $file) -notmatch $Pattern) {
34                        Write-Verbose -Message "The file: $file was successfully modified."
35                    }
36                    else {
37                        Write-Verbose -Message "Attempt to modify the file: $file was unsuccessful."
38                    }
39                }
40            }
41        }
42    }
43}

The path to the files can be provided via parameter or pipeline input:

1Get-ChildItem -Path C:\tmp\ConfigureNewServer | ConvertTo-MrMOFv4 -Verbose

dsc-error5a.jpg

Now that those two lines have been removed from the MOF file, let's try to apply the configuration again:

1Start-DscConfiguration -Path .\ConfigureNewServer -Wait -Verbose -Credential (Get-Credential)

dsc-error6.jpg

Success!

µ