How can I conditionally execute a resource in DSC Powershell based on the previous execution of the resource?
Let's say I have two blocks:
Configuration TestConfig
{
Node ('localhost') {
File foo {
DestinationPath = 'C:\foo.txt'
Contents = 'Bar'
}
Script dothing {
SetScript = {'Baz' | set-content C:\foo.txt}
TestScript = {return $false}
GetScript = {
return @{
GetScript = ''
SetScript = ''
TestScript = ''
Credential = $Credential
Result = (Invoke-Expression -Command $TestScript)
}
}
DependsOn = '[File]foo'
}
}
}
I built a test that does something like this, but it looks like the service resource starts regardless of the result of the file's test file - that is, if it actually did something to the file.
VERBOSE: [MACHINENAME]: LCM: [ Start Set ]
VERBOSE: [MACHINENAME]: LCM: [ Start Resource ] [[File]foo]
VERBOSE: [MACHINENAME]: LCM: [ Start Test ] [[File]foo]
VERBOSE: [MACHINENAME]: [[File]foo] The destination object was found and no action is required.
VERBOSE: [MACHINENAME]: LCM: [ End Test ] [[File]foo] in 0.0040 seconds.
VERBOSE: [MACHINENAME]: LCM: [ Skip Set ] [[File]foo]
VERBOSE: [MACHINENAME]: LCM: [ End Resource ] [[File]foo]
VERBOSE: [MACHINENAME]: LCM: [ Start Resource ] [[Script]dothing]
VERBOSE: [MACHINENAME]: LCM: [ Start Test ] [[Script]dothing]
VERBOSE: [MACHINENAME]: LCM: [ End Test ] [[Script]dothing] in 0.0050 seconds.
VERBOSE: [MACHINENAME]: LCM: [ Start Set ] [[Script]dothing]
VERBOSE: [MACHINENAME]: LCM: [ End Set ] [[Script]dothing] in 0.0060 seconds.
VERBOSE: [MACHINENAME]: LCM: [ End Resource ] [[Script]dothing]
VERBOSE: [MACHINENAME]: LCM: [ End Set ]
VERBOSE: [MACHINENAME]: LCM: [ End Set ] in 0.0390 seconds.
Unlike the example, how can I only use the Script resource if the File resource actually did something?
My use case checks if a file has been modified in a remote location, and if so, copy it to the local computer and then restart the service. I obviously don't want to restart the service if the file hasn't changed, but I don't see a good idempotent way to do this since the file is checked using a hash - I will need to have my service restart the step, do the same file hash check.
source to share
I don't think DSC has a built-in DSC that allows one resource to determine if the "Set-TargetResource" function was actually executed on another resource in the same configuration run.
However, the following Script resources will do what you want, but they use a script level global variable for communication, so they will break if you have multiple services to configure into a single DSC. The ConfigureService Script resource updates the local configuration file and sets a flag that the RestartService resource reads to check if a change has been made. It's a little messy, but it works.
Script ConfigureService {
GetScript = { return $null; }
TestScript = {
# compare remote and local file contents and return
# $true if they match, or $false if they're different
$source = "D:\temp\dsctest\source.txt";
$target = "D:\temp\dsctest\target.txt";
$isMatch = [System.IO.File]::ReadAllText($target) -eq [System.IO.File]::ReadAllText($source);
# set a flag for the RestartService resource to read
$script:serviceChanged = -not $isMatch;
write-verbose "isMatch = $isMatch";
return $isMatch;
}
SetScript = {
# overwrite the local file
$source = "D:\temp\dsctest\source.txt";
$target = "D:\temp\dsctest\target.txt";
$content = [System.IO.File]::ReadAllText($source);
write-verbose "overwriting config";
[System.IO.File]::WriteAllText($target, $content);
}
}
Script RestartService {
DependsOn = "[Script]ConfigureService"
GetScript = { return $null; }
TestScript = {
write-verbose "serviceChanged = $($script:serviceChanged)";
return -not $script:serviceChanged;
}
SetScript = {
# restart the service
write-verbose "restarting service";
}
}
Alternatively, just roll it all up into one resource. If you want to reuse this for multiple services, you can move it into a fully fledged custom ServiceConfiguration resource.
Script ConfigureService {
GetScript = { return $null; }
TestScript = {
# compare remote and local file contents and return
# $true if they match, or $false if they're different
$source = "D:\temp\dsctest\source.txt";
$target = "D:\temp\dsctest\target.txt";
$isMatch = [System.IO.File]::ReadAllText($target) -eq [System.IO.File]::ReadAllText($source);
write-verbose "isMatch = $isMatch";
return $isMatch;
}
SetScript = {
# overwrite the local file
$source = "D:\temp\dsctest\source.txt";
$target = "D:\temp\dsctest\target.txt";
$content = [System.IO.File]::ReadAllText($source);
write-verbose "overwriting config";
[System.IO.File]::WriteAllText($target, $content);
# restart the service
write-verbose "restarting service";
}
}
source to share