Skip to content

Get the source code of the PowerShell cmdlets

Example:

$commandName = "Write-Host";
 
(Get-Command $commandName).DLL;
(Get-Command $commandName).ImplementingType;

Then you can use a decompiler to look at the code, eg. Reflector, ILSpy etc.

Advertisements

Running and testing DSC configurations “manually”

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;

.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.