DSC - Feature dependancy

Hello,

Even though DSC is not new, I just heard about it this week and I’ve been playing with it since.

Just before discovering DSC, I actually started to learn Ansible so I was happy to have found this.

 

My question is about feature dependencies.

Say I want to remove the previously installed dhcp-server role from a list of servers, I could do this:

Node $AllNodes {

WindowsFeature dhcp-server {

Name = 'DHCP'

Ensure = 'Present'

}

WindowsFeature dhcp-server-tools {

DependsON = '[WindowsFeature]dhcp-server'

Name = 'RSAT-DHCP'

Ensure = 'Absent'

}

xDhcpServerScope Scope {

DependsON = '[WindowsFeature]dhcp-server'

Ensure = 'Absent'

Name = 'PowerShellScope'

ScopeID = '192.168.1.0'

IPStartRange = '192.168.1.10'

IPEndRange = '192.168.1.100'

SubnetMask = '255.255.255.0'

LeaseDuration = '00:08:00'

State = 'Active'

AddressFamily = 'IPv4'

}

What would happen if some of the servers in the list don’t even have it installed?

The first part won’t matter because it will just do nothing.

The removal of Scope though will return an error. It can’t check if the scope is present, if Dhcp-server role is not installed.

 

I think there should be a way to make it dependent on the state of a parent feature. Like if dhcp-server is present, execute next section.

I know there is the DependsOn parameter but this doesn’t help since it’s only affecting the order. It will just run after the execution of dhcp-server feature but if it wasn’t present in the first place, this will still pop up an error.

PowerShell DSC resource MSFT_xDhcpServerScope  failed to execute Test-TargetResource functionality with error message: Please ensure that the PowerShell module for role DHCPServer is installed

+ CategoryInfo          : InvalidOperation: (:) [], CimException

+ FullyQualifiedErrorId : ProviderOperationExecutionFailure

 

I Googled this for an answer or a solution but I feel no one is having the same question, making me question myself and my methods. Is there a better way to do this? Do I need to write a custom resource? Do I need to wait for this to be implemented?

Thank you

Steve

DSC is idempotent, means it will do only if the current state is not equal to the expected state. So it will not throw error. The error that you are getting is because the module containing xDhcpServerScope DSC resource is not available in the destination node. Once you install xDHCPServer module, you will not get this error.

But still as you said, there is no way to set conditions based on the state of another resource.

Hey,

I’m afraid that’s not true.

Remote Node:

I simplified the script to make a point:

Configuration TestLab

{

param ()

Import-DscResource –ModuleName "PSDesiredStateConfiguration"

Import-DscResource -ModuleName "xDhcpServer"

Node $AllNodes.Where{$_.Role -contains "Primary DC"}.NodeName {

 

xDhcpServerScope Scope {

Ensure = 'Absent'

Name = 'PowerShellScope'

ScopeID = '192.168.1.0'

IPStartRange = '192.168.1.10'

IPEndRange = '192.168.1.100'

SubnetMask = '255.255.255.0'

LeaseDuration = '00:08:00'

State = 'Active'

AddressFamily = 'IPv4'

}

}

}

#Create the MOF file based on the configuration above, using the MyData data-file

TestLab  -ConfigurationData "C:\users\Administrator\Documents\MyLab\MyData.psd1"

Start-DscConfiguration -wait -force -path "C:\Users\Administrator\TestLab" -verbose

All I’m doing here is importing the xDhcpServer module and trying to remove the DHCP scope, which doesn’t exist because there isn’t even a DHCP server installed.

PS C:\Users\Administrator> C:\Users\Administrator\Documents\MyLab\MyScript2.ps1

Directory: C:\Users\Administrator\TestLab

Mode                LastWriteTime         Length Name                                                                                    ----                -------------         ------ ----                                                                                                        -a----        6/09/2018     11:16           2400 ZMTPSDSCLAB1.mof                                                          VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguratio

n'.

VERBOSE: An LCM method call arrived from computer ZMTPSDSCLAB2 with user sid S-1-5-21-2605852739-2061032376-1152750048-500.

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ Start  Set      ]

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ Start  Resource ]  [[xDhcpServerScope]Scope]

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ Start  Test     ]  [[xDhcpServerScope]Scope]

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ End    Test     ]  [[xDhcpServerScope]Scope]  in 0.0470 seconds.

PowerShell DSC resource MSFT_xDhcpServerScope  failed to execute Test-TargetResource functionality with error message: Please ensure that the PowerShell module for role DHCPServer is installed

+ CategoryInfo          : InvalidOperation: (:) [], CimException

+ FullyQualifiedErrorId : ProviderOperationExecutionFailure

+ PSComputerName        : ZMTPSDSCLAB1

 

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ End    Set      ]

The SendConfigurationApply function did not succeed.

+ CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException

+ FullyQualifiedErrorId : MI RESULT 1

+ PSComputerName        : ZMTPSDSCLAB1

VERBOSE: Operation 'Invoke CimMethod' complete.

VERBOSE: Time taken for configuration job to complete is 1.829 seconds

 

If I run the exact same script but instead of removing a non-existent scope, adding the Dhcp-server, it works, which proves it’s not a missing dsc-resource issue.

Configuration TestLab

{

param ()

Import-DscResource –ModuleName "PSDesiredStateConfiguration"

Import-DscResource -ModuleName "xDhcpServer"

Node $AllNodes.Where{$_.Role -contains "Primary DC"}.NodeName {

WindowsFeature dhcp-server {

Name = 'DHCP'

Ensure = 'Present'

IncludeAllSubFeature = $true

}

 

WindowsFeature RSAT-Dhcp {

Name = 'RSAT-DHCP'

Ensure = 'Present'

DependsON = '[WindowsFeature]dhcp-server'

}

}

}

#Create the MOF file based on the configuration above, using the MyData data-file

TestLab  -ConfigurationData "C:\users\Administrator\Documents\MyLab\MyData.psd1"

Start-DscConfiguration -wait -force -path "C:\Users\Administrator\TestLab" -verbose

 

PS C:\Users\Administrator> C:\Users\Administrator\Documents\MyLab\MyScript2.ps1

Directory: C:\Users\Administrator\TestLab

Mode                LastWriteTime         Length Name                                                                                    ----                -------------         ------ ----                                                                                                        -a----        6/09/2018     11:26           2878 ZMTPSDSCLAB1.mof

VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguratio

n'.

VERBOSE: An LCM method call arrived from computer ZMTPSDSCLAB2 with user sid S-1-5-21-2605852739-2061032376-1152750048-500.

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ Start  Set      ]

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ Start  Resource ]  [[WindowsFeature]dhcp-server]

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ Start  Test     ]  [[WindowsFeature]dhcp-server]

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]dhcp-server] The operation 'Get-WindowsFeature' started: DHCP

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]dhcp-server] The operation 'Get-WindowsFeature' succeeded: DHCP

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ End    Test     ]  [[WindowsFeature]dhcp-server]  in 0.4840 seconds.

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ Start  Set      ]  [[WindowsFeature]dhcp-server]

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]dhcp-server] Installation started...

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]dhcp-server] Continue with installation?

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]dhcp-server] Prerequisite processing started...

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]dhcp-server] Prerequisite processing succeeded.

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]dhcp-server] Installation succeeded.

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]dhcp-server] Successfully installed the feature DHCP.

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ End    Set      ]  [[WindowsFeature]dhcp-server]  in 24.9220 seconds.

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ End    Resource ]  [[WindowsFeature]dhcp-server]

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ Start  Resource ]  [[WindowsFeature]RSAT-Dhcp]

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ Start  Test     ]  [[WindowsFeature]RSAT-Dhcp]

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]RSAT-Dhcp] The operation 'Get-WindowsFeature' started: RSAT-DHCP

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]RSAT-Dhcp] The operation 'Get-WindowsFeature' succeeded: RSAT-DHCP

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ End    Test     ]  [[WindowsFeature]RSAT-Dhcp]  in 0.2190 seconds.

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ Start  Set      ]  [[WindowsFeature]RSAT-Dhcp]

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]RSAT-Dhcp] Installation started...

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]RSAT-Dhcp] Continue with installation?

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]RSAT-Dhcp] Prerequisite processing started...

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]RSAT-Dhcp] Prerequisite processing succeeded.

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]RSAT-Dhcp] Installation succeeded.

VERBOSE: [ZMTPSDSCLAB1]:                            [[WindowsFeature]RSAT-Dhcp] Successfully installed the feature RSAT-DHCP.

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ End    Set      ]  [[WindowsFeature]RSAT-Dhcp]  in 7.9680 seconds.

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ End    Resource ]  [[WindowsFeature]RSAT-Dhcp]

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ End    Set      ]

VERBOSE: [ZMTPSDSCLAB1]: LCM:  [ End    Set      ]    in  33.8280 seconds.

VERBOSE: Operation 'Invoke CimMethod' complete.

VERBOSE: Time taken for configuration job to complete is 36.21 seconds

 

If right after this, I remove the dhcp-service and remove the scope, that still doesn’t exist, it will give the same error.

DSC is only idempotent if you write it that way, not by design.

You have the answer.

If right after this, I remove the dhcp-service and remove the scope, that still doesn't exist, it will give the same error.

If you are removing the Role, then I believe no need to remove the scope as there is no one to use it, else you remove the scope first then remove the Role.