DSC conditionally execute a resource based on another resource

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Justin King Justin King 7 months, 2 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35309
    Profile photo of Pete
    Pete
    Participant

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of Justin King
    Justin King
    Participant

    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.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.