DSC conditionally execute a resource based on another resource

Welcome Forums DSC (Desired State Configuration) DSC conditionally execute a resource based on another resource

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
2 years, 9 months ago.

  • Author
    Posts
  • #35309

    Participant
    Points: 0
    Rank: Member

    Hi

    I've not been able to find anything regarding this in "The DSC Book". I would like to reach out to everyone to see if its possible. I wish to execute a resource in Powershell DSC based on a previous resource executing.

    Basically, we have DSC configuration which ensures a our program package is installed, our config file is pulled down, and ensure the service is running. This is great!

    My problem starts if we update our configuration file, DSC would update the file, but not restart the service. So is there a way to do this with DSC?

    I managed to find someone posting something similar back in early 2015 so i was hoping perhaps there is a better way?

    http://stackoverflow.com/questions/26106187/how-do-i-conditionally-execute-a-resource-in-powershell-dsc-based-on-a-previous

  • #35311

    Member
    Points: 0
    Rank: Member

    At this point, no. What I've had to do in that same situation is either write a custom resource, or just use Script, that will combine the work of updating the config file and stopping or restarting the service if needed. For example:

            Script NSClientConf {
                TestScript = {
                    if (-not (Test-Path -LiteralPath $using:nsclientPath -PathType Leaf))
                    {
                        return $false
                    }
    
                    $content = [System.IO.File]::ReadAllText($using:nsclientPath)
    
                    if ($content -ne $using:nsclientConf)
                    {
                        return $false
                    }
    
                    return $true
                }
    
                SetScript = {
                    [System.IO.File]::WriteAllText($using:nsclientPath, $using:nsclientConf, [System.Text.Encoding]::ASCII)
    
                    $svc = Get-Service nscp -ErrorAction Ignore
    
                    if ($null -ne $svc -and $svc.Status -eq 'Running')
                    {
                        Stop-Service nscp -ErrorAction Stop
                    }
                }
    
                GetScript = {
                    $hash = @{
                        Result = ''
                    }
    
                    if (Test-Path -LiteralPath $using:nsclientPath -PathType Leaf)
                    {
                        $hash['Result'] = [System.IO.File]::ReadAllText($using:nsclientPath)
                    }
    
                    return $hash
                }
    
                DependsOn = @('[cPackage]NsClientPlusPlus')
            }
    
            Service nsclientSvc
            {
                Name      = 'nscp'
                State     = 'Running'
                
                DependsOn = @('[cPackage]NsClientPlusPlus', '[Script]NsClientConf')
            }
    

    It's ugly, and I hope we can improve on this sort of thing in the future.

  • #35348

    Participant
    Points: 0
    Rank: Member

    I'd just like to voice my support to the custom resource. It sounds like you may already be using one (when you reference it pulling your config file), so really just add logic that if it makes any changes it restarts the service.

    I just put a simple $ServiceRestart=$false at the beginning of the code and flip the value to $true anytime the custom resource finds it needs to make an update .. then add a simple restart service condition at the end of the set-targetresource.

The topic ‘DSC conditionally execute a resource based on another resource’ is closed to new replies.