Overview
ConfigurationSet represents a collection of configuration units that define a desired system state. Configuration sets are typically authored in YAML format and can be loaded, applied, tested, and serialized.
Class Definition
runtimeclass ConfigurationSet
{
ConfigurationSet();
// Identification
String Name;
String Origin;
String Path;
Guid InstanceIdentifier { get; };
// State
ConfigurationSetState State { get; };
DateTime FirstApply { get; };
DateTime ApplyBegun { get; };
DateTime ApplyEnded { get; };
// Content
IVector<ConfigurationUnit> Units;
// Schema
String SchemaVersion;
Uri SchemaUri;
// Metadata
ValueSet Metadata;
IVector<ConfigurationParameter> Parameters;
ValueSet Variables;
// Environment
ConfigurationEnvironment Environment { get; };
IVector<ConfigurationEnvironment> GetUnitEnvironments();
// Events
event TypedEventHandler<ConfigurationSet, ConfigurationSetChangeData> ConfigurationSetChange;
// Operations
void Serialize(IOutputStream stream);
void Remove();
}
Properties
Name
Name of the configuration set (often the filename).
var configSet = new ConfigurationSet()
{
Name = "DevEnvironmentSetup",
Origin = "https://github.com/contoso/configs"
};
Origin
Source location (e.g., Git repository URL).
Path
Local filesystem path where the configuration file is located.
For historical configuration sets, the file may no longer exist or may have been modified.
InstanceIdentifier
Unique identifier for this configuration set instance on the system.
The InstanceIdentifier may change when reapplying a configuration set from history if parts of the set have changed.
State
Current state of the configuration set.
ConfigurationSetState values:
Unknown - State is unknown (not applied)
Pending - Recorded in history but not yet applied
InProgress - Currently being applied
Completed - Application completed
var configSet = openResult.Set;
switch (configSet.State)
{
case ConfigurationSetState.Unknown:
Console.WriteLine("Configuration not yet applied");
break;
case ConfigurationSetState.InProgress:
Console.WriteLine("Configuration is being applied");
break;
case ConfigurationSetState.Completed:
Console.WriteLine("Configuration completed");
Console.WriteLine($"Applied: {configSet.ApplyEnded}");
break;
}
Units
Units
IVector<ConfigurationUnit>
Collection of configuration units in this set.
See ConfigurationUnit for details.
var configSet = openResult.Set;
Console.WriteLine($"Configuration units: {configSet.Units.Count}");
foreach (var unit in configSet.Units)
{
Console.WriteLine($"- {unit.Type}");
Console.WriteLine($" Identifier: {unit.Identifier}");
Console.WriteLine($" Intent: {unit.Intent}");
Console.WriteLine($" State: {unit.State}");
if (unit.Dependencies.Count > 0)
{
Console.WriteLine($" Dependencies: {string.Join(", ", unit.Dependencies)}");
}
}
SchemaVersion and SchemaUri
Schema version string (e.g., “0.2”).
URI of the configuration schema.
Setting SchemaVersion automatically updates SchemaUri and vice versa.
Additional metadata properties for the configuration set.
var configSet = new ConfigurationSet();
// Add custom metadata
configSet.Metadata["Author"] = "DevOps Team";
configSet.Metadata["Version"] = "1.0.0";
configSet.Metadata["Environment"] = "Development";
// Read metadata
if (configSet.Metadata.ContainsKey("Author"))
{
var author = configSet.Metadata["Author"] as string;
Console.WriteLine($"Author: {author}");
}
Parameters
Parameters
IVector<ConfigurationParameter>
Parameters that can be provided to customize configuration application.
var configSet = openResult.Set;
// List parameters
foreach (var param in configSet.Parameters)
{
Console.WriteLine($"Parameter: {param.Name}");
Console.WriteLine($" Description: {param.Description}");
Console.WriteLine($" Type: {param.Type}");
Console.WriteLine($" Required: {param.IsRequired}");
Console.WriteLine($" Secure: {param.IsSecure}");
if (param.DefaultValue != null)
{
Console.WriteLine($" Default: {param.DefaultValue}");
}
}
// Set parameter values
foreach (var param in configSet.Parameters)
{
if (param.Name == "InstallPath")
{
param.ProvidedValue = @"C:\MyApp";
}
else if (param.Name == "EnableFeatures")
{
param.ProvidedValue = true;
}
}
Variables
Variables used within the configuration set.
var configSet = openResult.Set;
// Access variables
if (configSet.Variables.ContainsKey("AppPath"))
{
var appPath = configSet.Variables["AppPath"] as string;
Console.WriteLine($"App Path: {appPath}");
}
// Set variables programmatically
configSet.Variables["CustomVariable"] = "CustomValue";
Methods
Serialize()
Writes the configuration set to a stream in YAML format.
Output stream to write the configuration.
using Windows.Storage;
using Windows.Storage.Streams;
// Save configuration to file
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"config.yml",
CreationCollisionOption.ReplaceExisting
);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
configSet.Serialize(stream.GetOutputStreamAt(0));
await stream.FlushAsync();
}
Console.WriteLine($"Configuration saved to: {file.Path}");
Remove()
Removes the configuration set from the recorded history.
// Get configuration from history
var processor = new ConfigurationProcessor(factory);
var history = await processor.GetConfigurationHistoryAsync();
foreach (var historicSet in history)
{
if (historicSet.Name == "ObsoleteConfig")
{
Console.WriteLine($"Removing: {historicSet.Name}");
historicSet.Remove();
}
}
This only removes the set from history tracking. It does not undo the configuration that was applied.
GetUnitEnvironments()
Gets the union of all environments defined by active units in the set.
Returns
IVector<ConfigurationEnvironment>
Collection of unique environments required by the configuration units.
var environments = configSet.GetUnitEnvironments();
Console.WriteLine($"Required environments: {environments.Count}");
foreach (var env in environments)
{
Console.WriteLine($"- Security Context: {env.Context}");
Console.WriteLine($" Processor: {env.ProcessorIdentifier}");
foreach (var prop in env.ProcessorProperties)
{
Console.WriteLine($" {prop.Key}: {prop.Value}");
}
}
Events
ConfigurationSetChange
Event fired when the state of the configuration set or any of its units changes.
configSet.ConfigurationSetChange += (sender, data) =>
{
Console.WriteLine($"Change Type: {data.Change}");
Console.WriteLine($"Set State: {data.SetState}");
if (data.Change == ConfigurationSetChangeEventType.UnitStateChanged)
{
Console.WriteLine($"Unit: {data.Unit.Type}");
Console.WriteLine($"Unit State: {data.UnitState}");
if (data.UnitState == ConfigurationUnitState.Completed)
{
if (data.ResultInformation.ResultCode != 0)
{
Console.WriteLine($"Failed: {data.ResultInformation.Description}");
}
else
{
Console.WriteLine("Succeeded");
}
}
}
};
See Events & Progress for more details.
Creating Configuration Sets
Programmatically
var configSet = new ConfigurationSet()
{
Name = "MyConfiguration",
Origin = "local",
SchemaVersion = "0.2"
};
// Add a configuration unit
var unit = new ConfigurationUnit()
{
Type = "Microsoft.WinGet.DSC/WinGetPackage",
Identifier = "installVSCode",
Intent = ConfigurationUnitIntent.Apply
};
unit.Settings["Id"] = "Microsoft.VisualStudioCode";
unit.Settings["Source"] = "winget";
configSet.Units.Add(unit);
// Add another unit with dependency
var unit2 = new ConfigurationUnit()
{
Type = "PSDscResources/Registry",
Identifier = "setRegistry",
Intent = ConfigurationUnitIntent.Apply
};
unit2.Dependencies.Add("installVSCode");
unit2.Settings["Key"] = @"HKCU\Software\MyApp";
unit2.Settings["ValueName"] = "InstallPath";
unit2.Settings["ValueData"] = @"C:\Program Files\VSCode";
configSet.Units.Add(unit2);
Console.WriteLine($"Created configuration with {configSet.Units.Count} units");
From YAML File
using Windows.Storage;
using Microsoft.Management.Configuration;
// Create processor
var factory = await ConfigurationStaticFunctions
.CreateConfigurationSetProcessorFactoryAsync("pwsh");
var processor = new ConfigurationProcessor(factory);
// Open file
var file = await StorageFile.GetFileFromPathAsync(
@"C:\Configs\setup.yml"
);
using (var stream = await file.OpenReadAsync())
{
var openResult = await processor.OpenConfigurationSetAsync(stream);
if (openResult.ResultCode == 0)
{
var configSet = openResult.Set;
Console.WriteLine($"Loaded: {configSet.Name}");
Console.WriteLine($"Units: {configSet.Units.Count}");
}
else
{
Console.WriteLine($"Failed to open: 0x{openResult.ResultCode:X}");
Console.WriteLine($"Field: {openResult.Field}");
Console.WriteLine($"Line: {openResult.Line}, Column: {openResult.Column}");
}
}
Example YAML Configuration
# yaml-language-server: $schema=https://aka.ms/configuration-dsc-schema/0.2
properties:
configurationVersion: 0.2.0
metadata:
author: DevOps Team
version: 1.0.0
parameters:
- name: InstallPath
type: string
description: Installation directory
defaultValue: "C:\\Apps"
resources:
- resource: Microsoft.WinGet.DSC/WinGetPackage
id: installGit
directives:
description: Install Git
settings:
Id: Git.Git
Source: winget
- resource: Microsoft.WinGet.DSC/WinGetPackage
id: installVSCode
dependsOn:
- installGit
directives:
description: Install VS Code
settings:
Id: Microsoft.VisualStudioCode
Source: winget
- resource: PSDscResources/Registry
id: configureRegistry
dependsOn:
- installVSCode
directives:
description: Configure registry settings
settings:
Key: HKCU\\Software\\MyApp
ValueName: InstallPath
ValueData: ${InstallPath}
Ensure: Present