Testing and configuration management in PowerShell: Pester and DSC
You test your scripts with Pester, describe server configuration as code with DSC and catch drift.
Level 401 · 12 lessons · 12 practice labs
Lessons
Pester
- First tests with Pester: Describe, It, Should
- Faking external dependencies with Mock
- Infrastructure tests: is the server as expected
- Code checks with PSScriptAnalyzer
- Short task: add tests and code checks to your earlier module
DSC
- What desired state configuration is
- Resources and writing a configuration
- Catching and correcting drift
- Applying a security baseline with DSC
- Short task: bring three servers to the same baseline
Wrap-up
- Running the tests on every commit with a Git hook
- Capstone: deliver a tested server configuration with drift checks
Practice labs
- Three tests for a function: Write Pester tests for the given subnet calculator function covering valid input, an edge value and a thrown error, and show they all pass.
- Is the bug in the code or the test?: Work out which of three failing tests come from a real code bug and which from scope and setup-block mistakes in the tests, fix the right side and show the run turns green.
- Is the server as expected?: Write infrastructure tests that verify services, listening ports, firewall rules and share permissions on the lab servers, and expose the server that breaks the baseline through the test output.
- A script with no warnings: Clear the PSScriptAnalyzer warnings about aliases, unused variables and a plain-text password parameter without changing the script’s behaviour, and show a clean run.
- Testing without touching the real service: Test the function that restarts a service and notifies an API using Mock, verifying no real call is made and the mocked commands are called once with the right parameters.
- No commit without passing tests and coverage: Set up a Git hook that runs the Pester tests and a code check with custom rules on every commit and rejects the commit if coverage is below the threshold, proving it with one failing and one passing commit.
- A first desired-state configuration: Write a DSC configuration that ensures a service is running and a configuration file exists with set content, apply it and show the test command returns compliant.
- A server changed by hand: On a server someone changed by hand, list the resources that drifted from the desired state with a detailed test, correct the drift and set the local configuration manager to auto-correct.
- Two roles, one baseline: Apply the same security baseline to two servers with different roles using configuration data, fix the configuration that fails on one because of resource order with dependency declarations, and prove both are compliant.
- List the settings that miss the baseline: Run the given security baseline configuration against the server in test-only mode without applying it, and give the non-compliant settings as a table with resource name, expected value and found value.
- Three servers, three different drifts: Find the settings that drifted from the baseline on Lavinya Klinikleri’s three lab servers, bring all three to the same state with one configuration and show that a second run changes no resource.
- A tested baseline with a drift report: Add Pester tests that verify every setting to a baseline configuration covering password, audit-log and service settings, break one setting by hand and prove the scheduled check writes the drift to a report with the date and the old value.
About this course
You test your scripts with Pester, describe server configuration as code with DSC and catch drift.
What you will learn
Prerequisites
You should have practised the topics of the same path up to level 301.
Lab environment
Lessons run in a Kali Linux terminal in the browser and in a practice domain (CORP.LAB) set up separately for every lab. Nothing to install.
Frequently asked questions
- Is this course right for my level?
- The course is at level 401; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.