0
0
PowerShellscripting~10 mins

Desired State Configuration (DSC) basics in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Desired State Configuration (DSC) basics
Write DSC Configuration Script
Compile Configuration -> MOF File
Apply MOF to Target Node
DSC Agent Checks Current State
Compare Current State with Desired State
Do Nothing
Report Success
DSC runs by writing a configuration script, compiling it to a MOF file, then applying it to a target node where the DSC agent ensures the system matches the desired state.
Execution Sample
PowerShell
Configuration SampleConfig {
  Node localhost {
    File ExampleFile {
      DestinationPath = 'C:\\example.txt'
      Contents = 'Hello DSC!'
      Ensure = 'Present'
    }
  }
}

SampleConfig
Start-DscConfiguration -Path .\SampleConfig -Wait -Verbose
This script defines a DSC configuration to create a file with specific content and applies it to the local machine.
Execution Table
StepActionEvaluationResult
1Define Configuration SampleConfigScript parsedConfiguration block created
2Compile SampleConfigMOF file generatedSampleConfig.mof created
3Start-DscConfiguration with MOF pathDSC agent reads MOFDSC agent starts applying configuration
4Check if 'C:\example.txt' exists with content 'Hello DSC!'File missing or content differentFile created with correct content
5Verify file stateFile exists with correct contentDSC reports success
6EndNo further actionsConfiguration applied successfully
💡 DSC stops after confirming the system matches the desired state defined in the MOF file.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Configuration ScriptNot definedDefinedDefinedDefinedDefined
MOF FileNot createdCreatedCreatedCreatedCreated
DSC Agent StateIdleIdleApplyingApplyingIdle
File 'C:\example.txt'Does not existDoes not existDoes not existCreated with contentExists with content
Key Moments - 3 Insights
Why does DSC create a MOF file before applying configuration?
The MOF file is a compiled version of the configuration script that the DSC agent reads to know the desired state. This is shown in execution_table step 2 where the MOF file is generated before applying.
What happens if the file already exists with the correct content?
DSC detects the current state matches the desired state and does nothing, as seen in execution_table step 4 where the check would find the file present and skip creation.
Why does DSC report success after applying changes?
Because after making changes, DSC verifies the system matches the desired state, confirming success as shown in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of step 4?
AMOF file generated
BFile created with correct content
CDSC agent starts applying configuration
DNo further actions
💡 Hint
Check the 'Result' column in step 4 of the execution_table.
At which step does the DSC agent begin applying the configuration?
AStep 3
BStep 5
CStep 2
DStep 1
💡 Hint
Look at the 'Action' and 'Result' columns to find when the DSC agent starts.
If the file already exists with the correct content, what would change in the execution_table?
AStep 4 would show 'File created with correct content'
BStep 4 would show 'File missing or content different'
CStep 4 would show 'File exists with correct content' and no creation action
DStep 5 would be skipped
💡 Hint
Refer to the key_moments explanation about existing file state and step 4.
Concept Snapshot
Desired State Configuration (DSC) basics:
- Write a configuration script defining desired state.
- Compile script to MOF file.
- Apply MOF to target node.
- DSC agent compares current vs desired state.
- Makes changes if needed, then reports success.
- Ensures system stays in desired state automatically.
Full Transcript
Desired State Configuration (DSC) is a way to make sure a computer system stays in a specific setup you want. First, you write a configuration script that says what you want, like creating a file with certain content. Then, you compile this script into a MOF file, which is like a set of instructions. You apply this MOF file to the computer, and the DSC agent on that computer reads it. The agent checks if the current system matches what you want. If not, it fixes things, like creating the file. Once everything matches, DSC reports success and keeps the system in that state. This process helps keep computers consistent and saves time from fixing things manually.