Skip to content

Running and testing DSC configurations “manually”

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

This is the result, My-DSC-Configuration.ps1:

# $PSScriptRoot is the directory where this script is located.
 
# Backup the original module-paths variable.
$originalModulePath = $env:PSModulePath;
 
# Add the directory where we are executing from to the module-paths. That way we can put our included DSC resources beside this script.
$env:PSModulePath = $env:PSModulePath + ";" + $PSScriptRoot;
 
Write-Host "Compile";
Write-Host "***********************************************************";
# Compiling the configuration to *.mof
MyDscConfiguration -OutputPath $PSScriptRoot -Verbose;
 
Write-Host;
Write-Host "Test-DscConfiguration";
Write-Host "***********************************************************";
# Test the configuration.
Test-DscConfiguration -Path $PSScriptRoot;
 
Write-Host;
Write-Host "Get-DscConfiguration";
Write-Host "***********************************************************";
# Test the configuration.
Get-DscConfiguration;
 
Write-Host;
Write-Host "Start-DscConfiguration";
Write-Host "***********************************************************";
# Run the configuration.
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;
 
# Reset the to the original module-paths.
$env:PSModulePath = $originalModulePath;
 
configuration MyDscConfiguration
{
	param
	(
		[string]$filePath = $PSScriptRoot.TrimEnd("\"+ "\Test.txt",
		[Parameter(Mandatory)]
		[string]$fileText
	)
 
	# To avoid warning. Will work without it.
	Import-DscResource -Module PSDesiredStateConfiguration;
 
	node localhost
	{
		Script CorrectFile
		{            
			GetScript = {
				if(Test-Path $using:filePath)
				{
					$content = Get-Content $using:filePath;
				}
 
				return @{ Result = $content };
			}
			SetScript = {
				Set-Content -Path $using:filePath -Value $using:fileText;
			}
			TestScript = {
				if(Test-Path $using:filePath)
				{
					$content = Get-Content $using:filePath;
 
					if($content -eq $using:fileText)
					{
						return $true;
					}
				}
 
				return $false;
			}
		}
	}
}

Open the script with Windows PowerShell ISE and run it.

I have tested it on a Windows Server 2016 Datacenter machine in Azure.

.NET Framework project with Visual Studio 2017 csproj

In Visual Studio 2017 you have the following project-templates (among others):

  • Class Library (.NET Core)
  • Class Library (.NET Framework)
  • Class Library (.NET Standard)

If you create a .NET Core or .NET Standard class-library you get the new “skinny” project-file-format. If you create a .NET Framework class-library you get the “old” format. But it is easy to change a clean .NET Core or .NET Standard project to a .NET Framework project. Just change the TargetFramework-tag in the project file.

.NET Core

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>
</Project>

.NET Standard

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
  </PropertyGroup>
</Project>

.NET Framework

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>
</Project>

A list of the target-frameworks is found here: Target frameworks

Other csproj-links

Where is the taskbar in Windows?

The taskbar in Windows is here:

  • C:\Users\USERNAME\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar

Found the answer here:

Taskbar in Swedish = Aktivitetsfältet

You can translate windows-terms here:

 

Change the network interface of a VM in Azure

I got help here:

What to do:

  1. Dissociate your public ip from your old network interface. Choose the public ip and click “Dissociate”.
  2. Create your new network interface.
  3. Then associate the public ip with the new network interface.
  4. Get the resource id of both the old and new network interface in Azure portal: Choose the Network interface -> Properties -> RESOURCE ID
  5. Then run the following Azure-PowerShell-command:

Get-AzureRMVM -ResourceGroupName [RESOURCE-GROUP] -Name [MACHINE-NAME] | Remove-AzureRmVMNetworkInterface -Id “[RESOURCE-ID-OLD-NI]” | Add-AzureRmVMNetworkInterface -Id “[RESOURCE-ID-OLD-NI]” | Update-AzureRmVM

An item with the same key has already been added

I am working on a Visual Studio VSIX-solution. When i tried to open the “source.extension.vsixmanifest” I got the error “An item with the same key has already been added”. After a while (some googling) I realized that I had projects in my solution with the same name. My solution structure:

  • Solution ‘VisualStudio-Templates’ (9 projects)
    • .Global
    • Assemblies
      • Common
        • Common
        • IntegrationTests
        • UnitTests
      • Templating
        • IntegrationTests
        • Templating
        • UnitTests
    • Templates
    • Package

I renamed the test-projects so the structure is this:

  • Solution ‘VisualStudio-Templates’ (9 projects)
    • .Global
    • Assemblies
      • Common
        • Common
        • Common.IntegrationTests
        • Common.UnitTests
      • Templating
        • Templating
        • Templating.IntegrationTests
        • Templating.UnitTests
    • Templates
    • Package

This solved the problem.

Missing XFormFolders in EPiServer

Last week I had problems with missing xform-folders. I do not know if it has something to do with upgrading to EPiServer 10. It’s after upgrading to 10 I stepped into the problem.

When you click on a xform-property you get to the xform-selector, ~/EPiServer/CMS/edit/XFormSelect.aspx?form=THE-FORM-GUID. Choosing the folder-option “All forms” made it crash with a NullReferenceException. It turned out that som xforms belonged to xform-folders that no longer existed.

Looking at EPiServer.XForms.XFormFolder with a decompiler gives that the root-folder guid is 89ED3722-9625-4de2-BEC6-ECDD162DCBDC. So I wrote the following SQL to find missing XFormFolders:

DECLARE @Guid UniqueIdentifier = '89ED3722-9625-4de2-BEC6-ECDD162DCBDC';
 
SELECT
	DISTINCT(Guid01)
FROM
	tblSystemBigTable
WHERE
	StoreName = 'XForms' AND
	Guid01 <> @Guid AND
	Guid01 NOT IN (SELECT [Guid] FROM tblBigTableIdentity WHERE StoreName = 'XFormFolders')
ORDER BY
	Guid01;

And to upgrade all references from the missing folders to the root-folder i used:

DECLARE @Guid UniqueIdentifier = '89ED3722-9625-4de2-BEC6-ECDD162DCBDC';
 
UPDATE
	tblSystemBigTable
SET
	Guid01 = @Guid
WHERE
	StoreName = 'XForms' AND
	Guid01 <> @Guid AND
	Guid01 NOT IN (SELECT [Guid] FROM tblBigTableIdentity WHERE StoreName = 'XFormFolders');

After that my problem was solved.

Reinstall NuGet-packages

When you, for example, change the target framework for your Visual Studio project/projects you may need to reinstall your NuGet-packages.

Links:

Examples:

  • Update-Package -Reinstall (reinstalls all packages in the solution)
  • Update-Package -Reinstall -Project Company.Function (or -ProjectName, reinstalls all packages in the specified project)
  • Update-Package -Reinstall MyPackage (reinstalls the package “MyPackage” in the whole solution)

Some other parameters:

  • -IgnoreDependencies (skips updating any of the package’s dependencies)
  • -WhatIf (shows what would happen when running the command without actually performing the update)