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).

configuration ConfigureNewServer {

Import-DscResource -ModuleName cMrComputerName

node 192.168.29.107 {

cMrComputerName Name {
ComputerName = 'Server01'
}
}
}

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:

ConfigureNewServer

dsc-error1.jpg

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

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

dsc-error2.jpg

Undefined property ConfigurationName _ At line:16, char:2_ _ Buffer:_ _onfigureNewServer";_ _};^_ _insta_ _ + CategoryInfo : SyntaxError: (root/Microsoft/…gurationManager:String) [], CimException_ _ + FullyQualifiedErrorId : MiClientApiError_Failed_ _ + 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:

#Requires -Version 4.0
function ConvertTo-MrMOFv4 {

[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipelineByPropertyName)]
[ValidateScript({Test-Path $_ -PathType Leaf -Include *.mof})]
[Alias('FullName')]
[string[]]$Path,

[Parameter(DontShow)]
[ValidateNotNullorEmpty()]
[string]$Pattern = '^\sName=.*;$|^\sConfigurationName\s=.*;$'
)

PROCESS {
foreach ($file in $Path) {

$mof = Get-Content -Path $file

if ($mof -match $Pattern) {
Write-Verbose -Message "PowerShell v4 compatibility problems were found in file: $file"

try {
$mof -replace $Pattern |
Set-Content -Path $file -Force -ErrorAction Stop
}
catch {
Write-Warning -Message "An error has occurred. Error details: $_.Exception.Message"
}
finally {
if ((Get-Content -Path $file) -notmatch $Pattern) {
Write-Verbose -Message "The file: $file was successfully modified."
}
else {
Write-Verbose -Message "Attempt to modify the file: $file was unsuccessful."
}
}
}
}
}
}

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

Get-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:

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

dsc-error6.jpg

Success!

µ