PowerShell Desired State Configuration
Desired State Configuration is a way to state the end result you want for a computer. You state the result, and the system works toward it.
State the end result you want
A desired state is one end result. This block writes the result to a file and reads it back.
Set-Content -Path "desired.txt" -Value "ready"
Get-Content -Path "desired.txt"Run it again, same result
Writing the same value twice leaves the file unchanged. That is idempotence in action.
Set-Content -Path "desired.txt" -Value "ready"
Set-Content -Path "desired.txt" -Value "ready"
Get-Content -Path "desired.txt"Compare current with desired
Drift is the gap between the two. This block checks a file against the desired value.
Set-Content -Path "desired.txt" -Value "ready"
$desired = "ready"
$current = Get-Content -Path "desired.txt"
if ($current -eq $desired) { "matches desired state" } else { "differs from desired state" }Example
$desired = "ready"
Set-Content -Path "desired.txt" -Value $desired
$current = Get-Content -Path "desired.txt"
if ($current -eq $desired) { "Desired state reached" } else { "Desired state not reached" }Key points
- Desired state: the end result you want a computer to have.
- Declaration: a statement of the end result, not a list of steps.
- Idempotent: running the same action again leaves the result unchanged.
- Drift: the gap between the current result and the desired result.
- DSC: a system that declares a desired state and brings a computer to it.
In this example, the script stores the desired value, writes it to a file, reads the file back, and reports that the desired state is reached.
Note
The DSC engine does not run on Linux PowerShell, so these lessons show the ideas with plain PowerShell.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
You run the same action twice and the result stays the same. What is this property called?