Wondering what is the best way to have some logic inside DSC resources without resorting to writing custom DSC Resource. Example is below.
I need to provide content
parameter for built in DSC Resourse File
. I can not put Function inside Configuration to return that value and does not seem to be able to put logic inside Content
tag either. What can be possible approach for this situation.
``` $filePath = Join-path -Path "$($env:programdata)" -ChildPath "docker\config\daemon.json"
$filePath = Join-path -Path `"$($env:programdata)`" -ChildPath "docker\config\daemon.json`"
if (test-Path ($filePath))
{) { $jsonConfig = get-content $filePath | convertfrom-json
$jsonConfig.graph = $graphLocation
$jsonConfig | convertto-json
}
else { @{ graphLocation = "$graphLocation"} | convertto-json
}
```
If you need the logic to run as part of the DSC job then you will need to resort to building a custom DSC resource. Remember all the DSC code will get compiled into a MOF file, and MOF files cannot run arbitrary PowerShell code. Thus inline functions will not be available during the job.
However, you can have logic that runs during the compilation phase. For instance, computing a property value that will be assigned to a DSC resource property.
Configuration
is ultimately just a function that takes a name and a script block as parameters, and it is valid in PowerShell to define a nested function, although it must be defined in the function scope prior to it being used.
Configuration MyConfig {
function ComplexLogic() {
"It works!"
}
Import-DscResource -ModuleName 'PSDesiredStateConfiguration'
Node localhost {
Log Example {
Message = ComplexLogic
}
}
}
You could also run a plain PowerShell script that computes values and then passes the values as arguments to the DSC configuration.