Advanced PowerShell: performance, runspaces and tool design
You measure performance on large data, manage concurrency with runspaces, use .NET directly and design safe PowerShell tools ready for release.
Level 401 · 13 lessons · 15 practice labs
Lessons
Performance
- Measuring with Measure-Command and finding bottlenecks
- Pipeline or loop: the difference on big data
- The cost of growing arrays: List and StringBuilder
- Short task: cut an hour-long report to five minutes
Concurrency and .NET
- Concurrency with runspace pools
- Thread safety and shared data
- Using .NET classes directly
- Short task: probe a hundred endpoints with bounded concurrency
Tool design
- Proper output: type names and format files
- Dynamic parameters and argument completers
- Safe tools: input validation, signing and least privilege
- A module release pipeline: test, sign, publish
- Capstone: release a fast, tested audit module
Practice labs
- Measure a slow script's bottleneck: Measure the sections of a report script with Measure-Command and submit the one that takes longest.
- Replace a growing array with a List: Rewrite code that grows an array with += in a loop using List and StringBuilder and measure the gain.
- Cut an hour-long report to five minutes: Rewrite a report that matches a big CSV with nested loops using hashtable lookups and confirm the result is unchanged.
- Compare pipeline and loop on a big file: Do the same filter with the pipeline, foreach and a .NET reader and tabulate times and memory use.
- Benchmark reliably by repeating the measurement: Measure two string-building methods ten times each with Measure-Command, set aside the warm-up effect of the first run and tabulate the average and worst times.
- Work out how timings scale with input size: Write a reusable benchmark function that measures three lookup methods with a thousand, ten thousand and a hundred thousand records; return time and memory results as objects and report from which size each method pulls ahead.
- Run the existing Pester tests and read the result: Run the audit module's test folder with Invoke-Pester and write the name of the failing test with its expected and actual values to the answer file.
- Write Pester tests for a function: Write Pester tests covering valid, invalid and empty input for a function in the audit module and make them all pass.
- Release the audit module as a tested version: Write a release script that tests the module, runs the analyser, bumps the version and publishes to a local repository.
- Probe endpoints with bounded concurrency: Probe a list of local endpoints with at most ten requests at a time and list those that do not answer.
- Fix a race condition in a parallel counter: Fix a counter that gives the wrong total when run in parallel using a thread-safe collection.
- Hash files with a runspace pool: Write a function that hashes thousands of files with a runspace pool, collects errors and disposes the pool properly.
- Give output a type name and default view: Add a type name to the objects a function returns and write a format file showing a four-column table by default.
- Add context-aware completion to a parameter: Add an argument completer to the service name parameter that suggests running services.
- Harden a tool with validation and least privilege: Close the path traversal through a tool's path parameter with validation and make it run with only the rights it needs.
About this course
You measure performance on large data, manage concurrency with runspaces, use .NET directly and design safe PowerShell tools ready for release.
What you will learn
- Performance
- Concurrency and .NET
- Tool design
Prerequisites
You should have practised the topics of the same path up to level 301.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
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.