Skip to main content

Getting started with DSC 3.0 – Part 3: Capturing the current state with dsc config export

In the previous post we wrote a configuration document for a single service by hand. That works when you know exactly what you want to manage.

But what if you inherit a machine and have no idea what's configured on it? Or you have one server that works fine and you want the others to look the same? Writing the document from scratch is slow and you will forget things.

That's what dsc config export is for.

Note: this post is part of a bigger series on Microsoft Desired State Configuration.

What does export do?

The export command inspects the system and generates a configuration document that contains every instance of the resources you ask for. The result is a normal configuration document, so you can use it with get, test and set like any other.

It's the reverse of what we did in Part 2. Instead of describing the state and applying it, you read the state and turn it into a description.

Step 1: Tell DSC what to export

The input document only lists the resource types you want. Save this as export.dsc.yaml:

$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: All services
  type: Microsoft.Windows/Service
  properties: {}

Remark: DSC ignores any properties you specify here. It only looks at the resource type. The document must still be valid, so keep properties in place.

Step 2: Run it

dsc config export --file export.dsc.yaml

The output is a new configuration document with one resource instance for every service registered on the machine, each with its full configuration: start type, status, logon account, executable path and so on.


Remark: in the console, DSC shows the result as YAML. If you redirect or capture the output, DSC switches to JSON. Use --output-format (or -o) to choose explicitly: json, pretty-json or yaml.

Step 3: Save it

Let's put the result in a file, as YAML:

dsc config export --file export.dsc.yaml --output-format yaml > baseline.dsc.yaml

Without the explicit format, you would get JSON in that file.


Remark: I had to change the name of the exported resources as the exported names were invalid:

You now have a snapshot of the machine. That's useful on its own: keep it in source control and you can compare it with a later export.

Step 4: Turn the export into a desired state

A full export is a description of what is, not what you want. You probably don't want to enforce the state of every service on the machine.

So treat the export as raw material:

  • Remove the instances you don't care about
  • Check the values you keep. status is the state at the moment of the export, for example
  • Replace hardcoded values with parameters, like we did in Part 2

Because the export is a configuration document, you can also filter it before saving. With PowerShell:

$export = dsc config export --file export.dsc.yaml | ConvertFrom-Json
$export.resources | Where-Object { $_.properties.startType -eq 'Automatic' }

That gives you the services that start automatically. Pick the ones that matter, put them in a document and use dsc config test and dsc config set as before.


The rules

  • Only resources that support export can be used. Not every resource does.
  • Each resource type can only be listed once in the input document. If you list a type twice, or a type that isn't exportable, DSC returns an error.
  • The command only reads. It never changes anything on the system.

Tip: if you only want to export one resource type, you can skip the document and call the resource directly:

dsc resource export --resource Microsoft.Windows/Service

Same output, one command less.

Up to the next one!

More information

Popular posts from this blog

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Cache stampede: when our cache turned against us

While investigating some performance issues, we ran into an ASP.NET Core API that cached a fairly expensive aggregation query for 60 seconds. Under normal load, that was fine: one request rebuilds the cache, everyone else reads from it. Under peak load, dozens of requests would arrive in that same expiry window, all see a cache miss, and all fire the same expensive query in parallel. The database didn't like that. That was the moment when our caching layer stopped helping and started hurting. A burst of requests comes in at the same time, all miss the cache, and all go hammer the database or the downstream API at once. That's a cache stampede . The cache was supposed to protect our backend, and for a few hundred milliseconds it did the opposite. Why this happens IMemoryCache.GetOrCreate (and its async sibling) looks like it protects you, but it doesn't add any locking on its own. Look at the naive version: public async Task<Report> GetReportAsync(string key) ...

VS Code Planning mode

After the introduction of Plan mode in Visual Studio , it now also found its way into VS Code. Planning mode, or as I like to call it 'Hannibal mode', extends GitHub Copilot's Agent Mode capabilities to handle larger, multi-step coding tasks with a structured approach. Instead of jumping straight into code generation, Planning mode creates a detailed execution plan. If you want more details, have a look at my previous post . Putting plan mode into action VS Code takes a different approach compared to Visual Studio when using plan mode. Instead of a configuration setting that you can activate but have limited control over, planning is available as a separate chat mode/agent: I like this approach better than how Visual Studio does it as you have explicit control when plan mode is activated. Instead of immediately diving into execution, the plan agent creates a plan and asks some follow up questions: You can further edit the plan by clicking on ‘Open in Editor’: ...