Skip to content

Running and testing DSC configurations “manually”

August 15, 2017

I wanted to run and test my DSC configurations, for my Azure-Resource-Manager-Templates, “manually”.

Change and test the scripts with Windows PowerShell ISE. I have tested them on a Windows Server 2016 Datacenter machine in Azure.

First, PowerShell have to find your resource-modules:

  • Alternative 1: Copy all your modules to “C:\Program Files\WindowsPowerShell\Modules” or “C:\Program Files (x86)\WindowsPowerShell\Modules”.
  • Alternative 2: Add your resource-module path to $env:PSModulePath.

Alternative 2

$yourModulePath = "C:\DSC\YourModules";
 
# Backup the original module-paths variable.
$originalModulePath = $env:PSModulePath;
 
# Add your path.
$env:PSModulePath = $env:PSModulePath + ";" + $yourModulePath;
 
# Run/test your DSC-configurations.
# ...
 
# Reset to the original module-paths.
$env:PSModulePath = $originalModulePath;

1 Compile your configurations

Before you can test your configuration, you have to compile it into a MOF document.

If you have [PSCredential]-parameters in your configuration you have to allow it. See the second example.

1.1 Examples

1.1.1 My-DSC-Configuration.ps1

configuration MyDscConfiguration
{
	param
	(
		[Parameter(Mandatory)]
		[string]$myParameter
	)
	
	Import-DscResource -Module YourModule, PSDesiredStateConfiguration;
	
	node localhost
	{
		LocalConfigurationManager
		{
			RebootNodeIfNeeded = $true;
		}
	
		YourModuleResource YourModuleResource
		{
			MyProperty = $myParameter;
		}
	}
}
 
# Above is your configuration (paste it from your configuration-file). Below is for compiling.
 
MyDscConfiguration -MyParameter "Test" -OutputPath $PSScriptRoot -Verbose;

1.1.2 My-DSC-Configuration-With-Credentials.ps1

configuration MyDscConfigurationWithCredentials
{
	param
	(
		[Parameter(Mandatory)]
		[PSCredential]$credentials
	)
	
	Import-DscResource -Module YourModule, PSDesiredStateConfiguration;
	
	node localhost
	{
		LocalConfigurationManager
		{
			RebootNodeIfNeeded = $true;
		}
	
		YourModuleResource YourModuleResource
		{
			Credentials = $credentials;
		}
	}
}
 
# Above is your configuration (paste it from your configuration-file). Below is for compiling.
 
# Configure to allow domain-credentials.
$configuration = @{
	AllNodes = @(
		@{
			NodeName = "localhost";
			PSDscAllowDomainUser = $true;
			PSDscAllowPlainTextPassword = $true;
		}
	)
}
 
$credentials = Get-Credential -Message "Password please" -UserName "yourdomain.net\User-name";
 
MyDscConfigurationWithCredentials -ConfigurationData $configuration -Credentials $credentials -OutputPath $PSScriptRoot -Verbose;

2 Run/test – Run.ps1

# The $PSScriptRoot variable is where this script is located.
# If you use this variable this script must be located in the same directory as your compiled-configuration-mof files.
# Otherwise change the -Path parameter to where your compiled-configuration-mof files are.
 
# Get the configuration.
# https://docs.microsoft.com/en-us/powershell/module/psdesiredstateconfiguration/get-dscconfiguration/
Get-DscConfiguration;
 
# Test the configuration.
# https://docs.microsoft.com/en-us/powershell/module/psdesiredstateconfiguration/test-dscconfiguration/
Test-DscConfiguration -Path $PSScriptRoot;
 
# Run the configuration.
# https://docs.microsoft.com/en-us/powershell/module/psdesiredstateconfiguration/start-dscconfiguration/
Start-DscConfiguration -Path $PSScriptRoot -Wait -Verbose;
# If you get an error: "A configuration is pending."
# You can use the following command:
# Start-DscConfiguration -Path $PSScriptRoot -Force -Wait -Verbose;
Advertisements

From → DSC, PowerShell

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: